@typedly/array v3.0.0
typedly/array
A TypeScript type definitions package to handle array-related operations.
Table of contents
Installation
npm install @typedly/array --save-peer
Api
import {
AppendDuplicates,
AppendIfExists,
AppendUnique,
Append,
Fill,
First,
Includes,
Last,
Length,
Prepend,
Remove,
Reverse,
Unique
} from '@typedly/array';
Typedly
import { Typedly } from '@typedly/array';
type Example1 = Typedly.Array.Append.Unique<[1, 2], 3>;
const example1: Example1 = [1, 2, 3];
AppendDuplicates
import { AppendDuplicates } from '@typedly/array';
// Append 3 duplicates of "x" to the array [1, 2, 3]
type Example1 = AppendDuplicates<[1, 2, 3], "x", 3>; // [1, 2, 3, "x", "x", "x"]
const example1: Example1 = [1, 2, 3, "x", "x", "x"];
// Append 2 duplicates of 4 to the array [5, 6, 7]
type Example2 = AppendDuplicates<[5, 6, 7], 4, 2>; // [5, 6, 7, 4, 4]
const example2: Example2 = [5, 6, 7, 4, 4];
// Append 5 duplicates of true to the array [false, true]
type Example3 = AppendDuplicates<[false, true], true, 5>; // [false, true, true, true, true, true, true]
const example3: Example3 = [false, true, true, true, true, true, true];
AppendIfExists
import { AppendIfExists } from '@typedly/array';
// Append 3 duplicates of "x" to the array [1, 2, 3]
type Example1 = AppendDuplicates<[1, 2, 3], "x", 3>; // [1, 2, 3, "x", "x", "x"]
const example1: Example1 = [1, 2, 3, "x", "x", "x"];
// Append 2 duplicates of 4 to the array [5, 6, 7]
type Example2 = AppendDuplicates<[5, 6, 7], 4, 2>; // [5, 6, 7, 4, 4]
const example2: Example2 = [5, 6, 7, 4, 4];
// Append 5 duplicates of true to the array [false, true]
type Example3 = AppendDuplicates<[false, true], true, 5>; // [false, true, true, true, true, true, true]
const example3: Example3 = [false, true, true, true, true, true, true];
AppendUnique
import { AppendUnique } from '@typedly/array';
type Example1 = AppendUnique<[1, 2, 3], 4>; // [1, 2, 3, 4]
type Example2 = AppendUnique<[1, 2, 3], 2>; // [1, 2, 3] (unchanged)
const example1: Example1 = [1, 2, 3, 4];
const example2: Example2 = [1, 2, 3];
Append
import { Append } from '@typedly/array';
type Example1 = Append<[1, 2, 3], 4>; // [1, 2, 3, 4]
type Example2 = Append<[1, 2, 3], 2>; // [1, 2, 3, 2] (duplicates)
type Example3 = Append< [], 'x'>; // ['x']
type Example4 = Append<[1, 2], 2>; // [1, 2, 2]
type Example5 = Append<number[], string>; // [...number[], string]
const example1: Example1 = [1, 2, 3, 4];
const example2: Example2 = [1, 2, 3, 2];
const example3: Example3 = ['x'];
const example5: Example4 = [1, 2, 2];
const example6: Example5 = [1, 2, 3, 'a'];
Fill
import { Fill } from '@typedly/array';
type Example1 = Fill<[1, 2, 3], 0>; // [0, 0, 0]
const example1: Example1 = [0,0,0];
First
import { First } from '@typedly/array';
type Example1 = First<[1, 2, 3]>; // 1
type Example2 = First<[]>; // never
const example1: Example1 = 1;
// const example2: Example2;
Includes
import { Includes } from '@typedly/array';
type Example1 = Includes<[1, 2, 3], 2>; // true
type Example2 = Includes<[1, 2, 3], 4>; // false
const example1: Example1 = true;
const example2: Example2 = false;
Insert
import { Insert } from '@typedly/array';
// Insert 99 at index 2 in the array [1, 2, 3, 4]
type Example1 = Insert<[1, 2, 3, 4], 99, 2>; // [1, 2, 99, 3, 4]
const example1: Example1 = [1, 2, 99, 3, 4];
// Insert 'a' at index 1 in the array ['b', 'c']
type Example2 = Insert<['b', 'c'], 'a', 1>; // ['b', 'a', 'c']
const example2: Example2 = ['b', 'a', 'c'];
// Insert 'X' at index 0 in an empty array
type Example3 = Insert<[], 'X', 0>; // ['X']
const example3: Example3 = ['X'];
Last
import { Last } from '@typedly/array';
type Example1 = Last<[1, 2, 3]>; // 3
type Example2 = Last<[]>; // never
const example1: Example1 = 3;
// const example2: Example2;
Length
import { Length } from '@typedly/array';
type Example1 = Length<[1, 2, 3]>; // 3
type Example2 = Length<[]>; // 0
const example1: Example1 = 3;
const example2: Example2 = 0;
Prepend
import { Prepend } from '@typedly/array';
type Example1 = Append<[1, 2, 3], 4>; // [1, 2, 3, 4]
type Example2 = Append<[1, 2, 3], 2>; // [1, 2, 3, 2] (duplicates)
type Example3 = Append< [], 'x'>; // ['x']
type Example4 = Append<[1, 2], 2>; // [1, 2, 2]
type Example5 = Append<number[], string>; // [...number[], string]
const example1: Example1 = [1, 2, 3, 4];
const example2: Example2 = [1, 2, 3, 2];
const example3: Example3 = ['x'];
const example5: Example4 = [1, 2, 2];
const example6: Example5 = [1, 2, 3, 'a'];
Remove
import { Remove } from '@typedly/array';
type Example1 = Remove<[1, 2, 3], 2>; // [1, 3]
type Example2 = Remove<[1, 2, 3], 4>; // [1, 2, 3] (unchanged)
type Example3 = Remove<['a', 'b', 'a'], 'a'>; // ['b'] (removes all occurrences)
type Example4 = Remove<[], 'x'>; // [] (empty stays empty)
const example1: Example1 = [1, 3];
const example2: Example2 = [1, 2, 3];
const example3: Example3 = ['b'];
const example4: Example4 = [];
Reverse
import { Reverse } from '@typedly/array';
type Example1 = Reverse<[1, 2, 3]>; // [3, 2, 1]
type Example2 = Reverse<['c', 'b', 'a']>; // ['a', 'b', 'c']
const example1: Example1 = [3, 2, 1];
const example2: Example2 = ['a', 'b', 'c'];
ToUnion
import { ToUnion } from '@typedly/array';
type UnionFromArray1 = ToUnion<[1, 2, 3]>; // type UnionFromArray = 3 | 1 | 2
type UnionFromArray2 = ToUnion<["a", "b", "c"]>; // type UnionFromArray2 = "a" | "b" | "c"
const example1: UnionFromArray1 = 1;
const example2: UnionFromArray2 = "b";
Unique
import { Unique } from '@typedly/array';
type Example1 = Unique<[1, 2, 2, 3, 3, 3]>; // [1, 2, 3]
const example1: Example1 = [1, 2, 3];
Contributing
Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
Support
If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.
Support via:
Thanks for your support!
Code of Conduct
By participating in this project, you agree to follow Code of Conduct.
GIT
Commit
Versioning
Given a version number MAJOR.MINOR.PATCH, increment the:
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
FAQ How should I deal with revisions in the 0.y.z initial development phase?
The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.
How do I know when to release 1.0.0?
If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.
License
MIT © typedly (license)
Packages
- @typedly/callback: A TypeScript type definitions package for asynchronous and synchronous callback functions of various types.
- @typedly/character: A TypeScript type definitions package for various character types.
- @typedly/check: A lightweight TypeScript type definitions library for type comparison.
- @typedly/context: A TypeScript type definitions package for context data structures.
- @typedly/descriptor: A TypeScript type definitions package for property descriptor.
- @typedly/digit: A TypeScript type definitions package for digit types.
- @typedly/letter: A TypeScript type definitions package for handling letter types.
- @typedly/object: A TypeScript type definitions package to handle object-related operations.
- @typedly/payload: A TypeScript type definitions package for payload data structures.
- @typedly/property: A TypeScript type definitions package to handle object property-related operations.
- @typedly/regexp: A TypeScript type definitions package for
RegExp
. - @typedly/symbol: A TypeScript type definitions package for various symbols.