### Install @pallad/range via npm Source: https://github.com/pallad-ts/range/blob/master/README.md Instructions to install the `@pallad/range` library using the npm package manager. This command adds the library as a dependency to your project. ```shell npm install @pallad/range ``` -------------------------------- ### Create Range Instances Source: https://github.com/pallad-ts/range/blob/master/README.md Examples demonstrating how to create `Range` instances using `Range.create` for full, start, and end ranges. Also shows `Range.fromArray` for creating ranges from array or tuple inputs, including cases where extra values are ignored. ```typescript // full range Range.create(1, 100); // start range Range.create(1); // end range Range.create(undefined, 100); ``` ```typescript // start range Range.fromArray([1]) // full range Range.fromArray([1, 100]) // full range - other values are ignores Range.fromArray([1, 100, 1003, 3000]) // end range Range.fromArray([undefined, 100]) ``` -------------------------------- ### Handle Invalid Range Creation Source: https://github.com/pallad-ts/range/blob/master/README.md Illustrates scenarios where `TypeError` is thrown during `Range` creation due to invalid inputs. This includes cases with undefined/null values, a start value greater than the end, or empty/invalid arrays. ```typescript // fails - undefined values Range.create(undefined, null) // fails - start greater than end Range.create(100, 1) // fails - empty array Range.fromArray([]) // fails - undefined values only Range.fromArray([undefined, null]) ``` ```typescript Range.create(1, 100).value; Range.create(null, undefined).value // 'Cannot create Range from undefined or null values' ``` -------------------------------- ### Converting Range Types with Range.convert in TypeScript Source: https://github.com/pallad-ts/range/blob/master/README.md Illustrates the use of `Range.convert` to transform a range of one data type into a range of another type. It applies a mapping function to both the start and end values of the original range to produce a new range. ```typescript Range.convert(Range.create(1, 100), (value) => value + 's'); // Range { start: '1s', end: '100s' } Range.convert(Range.create(1), (value) => value + 's'); // Range { start: '1s'} Range.convert(Range.create(undefined, q00), (value) => value + 's'); // Range { end: '100s'} ``` -------------------------------- ### Controlling Range Exclusivity with Range.isWithin in TypeScript Source: https://github.com/pallad-ts/range/blob/master/README.md Demonstrates how to use `Range.isWithin` to check if a value falls within a defined range. It illustrates the default inclusive behavior and how to specify exclusive checks for the start, end, or both boundaries using the third argument. ```typescript const range = Range.create(1, 100); // exclusivity = false Range.isWithin(range, 100, false) // true Range.isWithin(range, 1, false) // true // same as above Range.isWithin(range, 100) // true Range.isWithin(range, 1) // true // exclusivity = true Range.isWithin(range, 100, true) // false Range.isWithin(range, 1, true) // false // start exclusive Range.isWithin(range, 100, {start: true}) // true Range.isWithin(range, 1, {start: true}) // false // end exclusive Range.isWithin(range, 100, {end: true}) // false Range.isWithin(range, 1, {end: true}) // true ``` -------------------------------- ### Using Custom Comparison for Range.convert in TypeScript Source: https://github.com/pallad-ts/range/blob/master/README.md Explains scenarios where a custom comparison function is necessary for `Range.convert`. This is particularly useful when the converted values (e.g., strings representing numbers) do not have a natural comparison order that aligns with the desired range logic, ensuring proper creation of the new range. ```typescript const range = Range.create(2, 100); // custom comparison function is needed since '2s' > '100s' using regular string comparison Range.convert(range, (value) => value + 's', (a, b) => parseInt(a) - parseInt(b)); ``` -------------------------------- ### Map Range to Custom Values Source: https://github.com/pallad-ts/range/blob/master/README.md Explains how `Range.map` can be used to transform a `Range` object into any other value. It supports mapping to simple values or using functions that receive the range properties to generate dynamic outputs. ```typescript const range = Range.create(1, 100); // mapping to simple values Range.map(range, {start: 'start', end: 'end', full: 'full'}) // 'full' ``` ```typescript // mapping functions enchantedRange.map({ start: ({start}) => `from ${start}`, end: ({end}) => `to ${end}`, full: ({start, end}) => `between ${start} and ${end}` }); // 'between 1 and 100` ``` -------------------------------- ### Enchant Range with Utility Methods Source: https://github.com/pallad-ts/range/blob/master/README.md Demonstrates how to 'enchant' a `Range` object to gain additional immutable utility methods. These methods include `isWithin` for checking value inclusion, `map` for transforming the range's representation, and `toTuple` for converting it to an array. ```typescript const enchantedRange = enchant(Range.create(1, 100)); enchantedRange.isWithin(40); // true enchantedRange.isWithin(500); // false enchantedRange.map({ start: ({start}) => `from ${start}`, end: ({end}) => `to ${end}`, full: ({start, end}) => `between ${start} and ${end}` }); // 'between 1 and 100` enchantedRange.toTuple(); // [1, 100] ``` -------------------------------- ### Define Core Range Interfaces and Types Source: https://github.com/pallad-ts/range/blob/master/README.md Defines the fundamental TypeScript interfaces and types for `Range.Start`, `Range.End`, `Range.Full`, and the union `Range` type. These types represent different forms of value ranges, from open-ended to fully bounded. ```typescript interface Start { start: T } ``` ```typescript interface End { end: T; } ``` ```typescript type Full = Start & End ``` ```typescript type Range = Range.Full | Range.Start | Range.End; ``` -------------------------------- ### Define Custom Comparators for Range Source: https://github.com/pallad-ts/range/blob/master/README.md Illustrates how to provide a custom comparison function to `Range.create` and `Range.fromArray` for handling complex boundary types. This allows for correct range validation and operations based on custom logic, such as comparing objects by a specific property. ```typescript Range.create({value: 1}, {value: 100}, (a, b) => a.value - b.value); // no fail Range.fromArray([{value: 1}, {value: 100}], (a, b) => a.value - b.value); // no fail ``` -------------------------------- ### Validate if Object is a Range Source: https://github.com/pallad-ts/range/blob/master/README.md Shows how to use `Range.is` to check if a given object conforms to the `Range` interface. This utility function can validate objects representing start-only, end-only, or full range structures. ```typescript Range.is({start: 10}) // true Range.is({end: 10}) // true Range.is({start: 1, end: 10}) // true ``` -------------------------------- ### Check if Value is Within Range Source: https://github.com/pallad-ts/range/blob/master/README.md Demonstrates the use of `Range.isWithin` to determine if a specific value falls within the defined boundaries of a `Range` object. This method leverages `@pallad/compare` and supports custom comparison functions for complex value types. ```typescript Range.isWithin(Range.create(1, 100), 50) // true Range.isWithin(Range.create(1, 100), 500) // false ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.