1.1.0 • Published 12 months ago
split-any v1.1.0
Split-Any
A function to split any type of value into pieces.
Install
npm i split-any
API
Currently, this function includes the following signatures.
split(str: string, separator: string | RegExp): string[]
Splits a string into chunks by the given separator.
assert.deepStrictEqual(
split("Hello, World!", ""),
["H", "e", "l", "l", "o", ",", " ", "W", "o", "r", "l", "d", "!"]
);
assert.deepStrictEqual(
split("Hello, World!", /,\s*/),
["Hello", "World!"]
);
split(str: string, length: number): string[]
Splits a string into chunks with the given length.
assert.deepStrictEqual(
split("Hello, World!", 3),
["Hel", "lo,", " Wo", "rld", "!"]
);
split(num: number, step: number): number[]
Splits a number into serials with the given step.
assert.deepStrictEqual(
split(100, 10),
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
);
assert.deepStrictEqual(
split(105, 10),
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 105]
);
split<T extends Array<any>>(arr: T, length: number): T[]
Splits an array into chunks of arrays with the given length.
assert.deepStrictEqual(
split(["Hello", "World", "Hi", "A-yon"], 2),
[["Hello", "World"], ["Hi", "A-yon"]]
);
split<T>(list: ArrayLike<T>, length: number): T[][]
Splits an array-like object into chunks of arrays with the given length.
/** @type {ArrayLike<number>} */
let args = null;
(function () { args = arguments; })(1, 2, 3, 4, 5, 6, 7, 8, 9);
assert.deepStrictEqual(
split(args, 2),
[[1, 2], [3, 4], [5, 6], [7, 8], [9]]
);
split<T extends Buffer | ArrayBufferLike | TypedArray>(buf: T, byteLength: number): T[]
Splits a Buffer, an ArrayBuffer, or a TypedArray into chunks with the given byteLength.
assert.deepStrictEqual(
split(Buffer.from(["Hello", "World", "Hi", "A-yon"]), 2),
[Buffer.from(["Hello", "World"]), Buffer.from(["Hi", "A-yon"])]
);
assert.deepStrictEqual(
split(Uint8Array.from([1, 2, 3, 4]), 2),
[Uint8Array.from([1, 2]), Uint8Array.from([3, 4])]
);
assert.deepStrictEqual(
split(new ArrayBuffer(8), 2),
[
new ArrayBuffer(2),
new ArrayBuffer(2),
new ArrayBuffer(2),
new ArrayBuffer(2)
]
);
split<T extends Buffer>(buf: T, separator: string | Buffer): T[]
Splits a Buffer into chunks by the given separator.
assert.deepStrictEqual(
split(Buffer.from("Hello, World, Hi, A-yon"), ", "),
[
Buffer.from("Hello"),
Buffer.from("World"),
Buffer.from("Hi"),
Buffer.from("A-yon")
]
);
assert.deepStrictEqual(
split(Buffer.from("Hello, World, Hi, A-yon"), Buffer.from(", ")),
[
Buffer.from("Hello"),
Buffer.from("World"),
Buffer.from("Hi"),
Buffer.from("A-yon")
]
);
split<T extends Set<any> | Map<any, any>>(collection: T, size: number): T[]
Splits a collection into chunks with the given size.
assert.deepStrictEqual(
split(new Set(["Hello", "World", "Hi", "A-yon"]), 2),
[new Set(["Hello", "World"]), new Set(["Hi", "A-yon"])]
);
assert.deepStrictEqual(
split(new Map([["Hello", "World"], ["Hi", "A-yon"]]), 1),
[new Map([["Hello", "World"]]), new Map([["Hi", "A-yon"]])]
);
split<T extends object>(obj: T extends Function ? never : T, size: number): Partial<T>[]
Splits an object into multiple objects with partial properties.
assert.deepStrictEqual(
split({ hello: "world", hi: "A-yon", foo: "bar" }, 2),
[
{ hello: "world", hi: "A-yon" },
{ foo: "bar" }
]
);
class Test {
constructor(data) {
Object.assign(this, data);
}
}
assert.deepStrictEqual(
split(new Test({ hello: "world", hi: "A-yon", foo: "bar" }), 2),
[
new Test({ hello: "world", hi: "A-yon" }),
new Test({ foo: "bar" })
]
);