@vltpkg/fast-split
Advanced tools
| /** utility types to turn a null/undefined/void return into string */ | ||
| export type NullToString<T> = VoidReplace<T>; | ||
| /** Utility type to replace null/undefined with a given type */ | ||
| export type NullReplace<T, R = string> = T extends NonNullable<T> ? T : NonNullable<T> | R; | ||
| /** Utility type to replace void with a given type */ | ||
| export type VoidReplace<T, R = string> = undefined extends T ? NullReplace<Exclude<T, void>, R> | R : NullReplace<T, R>; | ||
| /** | ||
| * Split a string by a string delimiter, optionally limiting the number | ||
| * of parts parsed, and/or transforming the string parts into some other | ||
| * type of value. | ||
| * | ||
| * Pass `-1` as the `limit` parameter to get all parts (useful if an `onPart` | ||
| * method is provided) | ||
| * | ||
| * If an `onPart` method is provided, and returns `undefined`, then the | ||
| * original string part is included in the result set. | ||
| * | ||
| * ```ts | ||
| * import { fastSplit } from '@vltpkg/fast-split' | ||
| * | ||
| * // say we want to split a string on '.' characters | ||
| * const str = getSomeStringSomehow() | ||
| * | ||
| * // basic usage, just like str.split('.'), gives us an array | ||
| * const parts = fastSplit(str, '.') | ||
| * | ||
| * // get just the first two parts, leave the rest intact | ||
| * // Note: unlike str.split('.', 3), the 'rest' here will | ||
| * // include the entire rest of the string. | ||
| * // If you do `str.split('.', 3)`, then the last item in the | ||
| * // returned array is truncated at the next delimiter | ||
| * const [first, second, rest] = fastSplit(str, '.', 3) | ||
| * | ||
| * // If you need to transform it, say if it's an IPv4 address | ||
| * // that you want to turn into numbers, you can do that by | ||
| * // providing the onPart method, which will be slightly faster | ||
| * // than getting an array and subsequently looping over it | ||
| * // pass `-1` as the limit to give us all parts | ||
| * const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(s)) | ||
| * ``` | ||
| */ | ||
| export declare function fastSplit<T = string>(str: string, delim: string, limit: number, onPart: (part: string, parts: NullToString<T>[], i: number) => T): NullToString<T>[]; | ||
| export declare function fastSplit(str: string, delim: string, limit?: number): string[]; | ||
| //# sourceMappingURL=index.d.ts.map |
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAA;AAE5C,+DAA+D;AAC/D,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,IACnC,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnD,qDAAqD;AACrD,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,IACnC,SAAS,SAAS,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,GACxD,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,EAClC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC,GAC/D,YAAY,CAAC,CAAC,CAAC,EAAE,CAAA;AACpB,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,CAAC,EAAE,MAAM,GACb,MAAM,EAAE,CAAA"} |
| export function fastSplit(str, delim, limit = -1, onPart) { | ||
| let i = 0; | ||
| let p = 0; | ||
| const l = delim.length; | ||
| const parts = []; | ||
| while (i !== -1) { | ||
| i = str.indexOf(delim, p); | ||
| const part = i === -1 || parts.length === limit - 1 ? | ||
| str.substring(p) | ||
| : str.substring(p, i); | ||
| parts.push((onPart?.(part, parts, i) ?? part)); | ||
| if (parts.length === limit) { | ||
| // push the rest into the last part | ||
| return parts; | ||
| } | ||
| p = i + l; | ||
| } | ||
| return parts; | ||
| } | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AA0DA,MAAM,UAAU,SAAS,CACvB,GAAW,EACX,KAAa,EACb,KAAK,GAAG,CAAC,CAAC,EACV,MAIkB;IAElB,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAA;IACtB,MAAM,KAAK,GAAsB,EAAE,CAAA;IACnC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QAChB,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;QACzB,MAAM,IAAI,GACR,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC;YACtC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;YAClB,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QACvB,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,IAAI,CAAoB,CAAC,CAAA;QACjE,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;YAC3B,mCAAmC;YACnC,OAAO,KAAK,CAAA;QACd,CAAC;QACD,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACX,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["/** utility types to turn a null/undefined/void return into string */\nexport type NullToString<T> = VoidReplace<T>\n\n/** Utility type to replace null/undefined with a given type */\nexport type NullReplace<T, R = string> =\n T extends NonNullable<T> ? T : NonNullable<T> | R\n\n/** Utility type to replace void with a given type */\nexport type VoidReplace<T, R = string> =\n undefined extends T ? NullReplace<Exclude<T, void>, R> | R\n : NullReplace<T, R>\n\n/**\n * Split a string by a string delimiter, optionally limiting the number\n * of parts parsed, and/or transforming the string parts into some other\n * type of value.\n *\n * Pass `-1` as the `limit` parameter to get all parts (useful if an `onPart`\n * method is provided)\n *\n * If an `onPart` method is provided, and returns `undefined`, then the\n * original string part is included in the result set.\n *\n * ```ts\n * import { fastSplit } from '@vltpkg/fast-split'\n *\n * // say we want to split a string on '.' characters\n * const str = getSomeStringSomehow()\n *\n * // basic usage, just like str.split('.'), gives us an array\n * const parts = fastSplit(str, '.')\n *\n * // get just the first two parts, leave the rest intact\n * // Note: unlike str.split('.', 3), the 'rest' here will\n * // include the entire rest of the string.\n * // If you do `str.split('.', 3)`, then the last item in the\n * // returned array is truncated at the next delimiter\n * const [first, second, rest] = fastSplit(str, '.', 3)\n *\n * // If you need to transform it, say if it's an IPv4 address\n * // that you want to turn into numbers, you can do that by\n * // providing the onPart method, which will be slightly faster\n * // than getting an array and subsequently looping over it\n * // pass `-1` as the limit to give us all parts\n * const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(s))\n * ```\n */\nexport function fastSplit<T = string>(\n str: string,\n delim: string,\n limit: number,\n onPart: (part: string, parts: NullToString<T>[], i: number) => T,\n): NullToString<T>[]\nexport function fastSplit(\n str: string,\n delim: string,\n limit?: number,\n): string[]\nexport function fastSplit<T = string>(\n str: string,\n delim: string,\n limit = -1,\n onPart?: (\n part: string,\n parts: NullToString<T>[],\n i: number,\n ) => T | undefined,\n): NullToString<T>[] {\n let i = 0\n let p = 0\n const l = delim.length\n const parts: NullToString<T>[] = []\n while (i !== -1) {\n i = str.indexOf(delim, p)\n const part =\n i === -1 || parts.length === limit - 1 ?\n str.substring(p)\n : str.substring(p, i)\n parts.push((onPart?.(part, parts, i) ?? part) as NullToString<T>)\n if (parts.length === limit) {\n // push the rest into the last part\n return parts\n }\n p = i + l\n }\n return parts\n}\n"]} |
+3
-17
| { | ||
| "name": "@vltpkg/fast-split", | ||
| "description": "A fast way to split small-to-medium sized strings by small string delimiters", | ||
| "version": "1.0.0-rc.10", | ||
| "version": "1.0.0-rc.11", | ||
| "repository": { | ||
@@ -11,13 +11,2 @@ "type": "git", | ||
| "author": "vlt technology inc. <support@vlt.sh> (http://vlt.sh)", | ||
| "tshy": { | ||
| "selfLink": false, | ||
| "liveDev": true, | ||
| "dialects": [ | ||
| "esm" | ||
| ], | ||
| "exports": { | ||
| "./package.json": "./package.json", | ||
| ".": "./src/index.ts" | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
@@ -29,3 +18,2 @@ "@eslint/js": "^9.39.1", | ||
| "tap": "^21.5.0", | ||
| "tshy": "^3.1.0", | ||
| "typedoc": "~0.27.9", | ||
@@ -43,3 +31,3 @@ "typescript": "5.7.3", | ||
| "prettier": "../../.prettierrc.js", | ||
| "module": "./dist/esm/index.js", | ||
| "module": "./dist/index.js", | ||
| "type": "module", | ||
@@ -50,4 +38,3 @@ "exports": { | ||
| "import": { | ||
| "types": "./dist/esm/index.d.ts", | ||
| "default": "./dist/esm/index.js" | ||
| "default": "./dist/index.js" | ||
| } | ||
@@ -68,5 +55,4 @@ } | ||
| "posttest": "tsc --noEmit", | ||
| "tshy": "tshy", | ||
| "typecheck": "tsc --noEmit" | ||
| } | ||
| } |
| /** utility types to turn a null/undefined/void return into string */ | ||
| export type NullToString<T> = VoidReplace<T>; | ||
| /** Utility type to replace null/undefined with a given type */ | ||
| export type NullReplace<T, R = string> = T extends NonNullable<T> ? T : NonNullable<T> | R; | ||
| /** Utility type to replace void with a given type */ | ||
| export type VoidReplace<T, R = string> = undefined extends T ? NullReplace<Exclude<T, void>, R> | R : NullReplace<T, R>; | ||
| /** | ||
| * Split a string by a string delimiter, optionally limiting the number | ||
| * of parts parsed, and/or transforming the string parts into some other | ||
| * type of value. | ||
| * | ||
| * Pass `-1` as the `limit` parameter to get all parts (useful if an `onPart` | ||
| * method is provided) | ||
| * | ||
| * If an `onPart` method is provided, and returns `undefined`, then the | ||
| * original string part is included in the result set. | ||
| * | ||
| * ```ts | ||
| * import { fastSplit } from '@vltpkg/fast-split' | ||
| * | ||
| * // say we want to split a string on '.' characters | ||
| * const str = getSomeStringSomehow() | ||
| * | ||
| * // basic usage, just like str.split('.'), gives us an array | ||
| * const parts = fastSplit(str, '.') | ||
| * | ||
| * // get just the first two parts, leave the rest intact | ||
| * // Note: unlike str.split('.', 3), the 'rest' here will | ||
| * // include the entire rest of the string. | ||
| * // If you do `str.split('.', 3)`, then the last item in the | ||
| * // returned array is truncated at the next delimiter | ||
| * const [first, second, rest] = fastSplit(str, '.', 3) | ||
| * | ||
| * // If you need to transform it, say if it's an IPv4 address | ||
| * // that you want to turn into numbers, you can do that by | ||
| * // providing the onPart method, which will be slightly faster | ||
| * // than getting an array and subsequently looping over it | ||
| * // pass `-1` as the limit to give us all parts | ||
| * const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(s)) | ||
| * ``` | ||
| */ | ||
| export declare function fastSplit<T = string>(str: string, delim: string, limit: number, onPart: (part: string, parts: NullToString<T>[], i: number) => T): NullToString<T>[]; | ||
| export declare function fastSplit(str: string, delim: string, limit?: number): string[]; | ||
| //# sourceMappingURL=index.d.ts.map |
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAA;AAE5C,+DAA+D;AAC/D,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,IACnC,CAAC,SAAS,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnD,qDAAqD;AACrD,MAAM,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,MAAM,IACnC,SAAS,SAAS,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,GACxD,WAAW,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;AAErB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,SAAS,CAAC,CAAC,GAAG,MAAM,EAClC,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC,GAC/D,YAAY,CAAC,CAAC,CAAC,EAAE,CAAA;AACpB,wBAAgB,SAAS,CACvB,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,KAAK,CAAC,EAAE,MAAM,GACb,MAAM,EAAE,CAAA"} |
| export function fastSplit(str, delim, limit = -1, onPart) { | ||
| let i = 0; | ||
| let p = 0; | ||
| const l = delim.length; | ||
| const parts = []; | ||
| while (i !== -1) { | ||
| i = str.indexOf(delim, p); | ||
| const part = i === -1 || parts.length === limit - 1 ? | ||
| str.substring(p) | ||
| : str.substring(p, i); | ||
| parts.push((onPart?.(part, parts, i) ?? part)); | ||
| if (parts.length === limit) { | ||
| // push the rest into the last part | ||
| return parts; | ||
| } | ||
| p = i + l; | ||
| } | ||
| return parts; | ||
| } | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AA0DA,MAAM,UAAU,SAAS,CACvB,GAAW,EACX,KAAa,EACb,KAAK,GAAG,CAAC,CAAC,EACV,MAIkB;IAElB,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAA;IACtB,MAAM,KAAK,GAAsB,EAAE,CAAA;IACnC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QAChB,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;QACzB,MAAM,IAAI,GACR,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC;YACtC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;YAClB,CAAC,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;QACvB,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,IAAI,CAAoB,CAAC,CAAA;QACjE,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,EAAE,CAAC;YAC3B,mCAAmC;YACnC,OAAO,KAAK,CAAA;QACd,CAAC;QACD,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IACX,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC","sourcesContent":["/** utility types to turn a null/undefined/void return into string */\nexport type NullToString<T> = VoidReplace<T>\n\n/** Utility type to replace null/undefined with a given type */\nexport type NullReplace<T, R = string> =\n T extends NonNullable<T> ? T : NonNullable<T> | R\n\n/** Utility type to replace void with a given type */\nexport type VoidReplace<T, R = string> =\n undefined extends T ? NullReplace<Exclude<T, void>, R> | R\n : NullReplace<T, R>\n\n/**\n * Split a string by a string delimiter, optionally limiting the number\n * of parts parsed, and/or transforming the string parts into some other\n * type of value.\n *\n * Pass `-1` as the `limit` parameter to get all parts (useful if an `onPart`\n * method is provided)\n *\n * If an `onPart` method is provided, and returns `undefined`, then the\n * original string part is included in the result set.\n *\n * ```ts\n * import { fastSplit } from '@vltpkg/fast-split'\n *\n * // say we want to split a string on '.' characters\n * const str = getSomeStringSomehow()\n *\n * // basic usage, just like str.split('.'), gives us an array\n * const parts = fastSplit(str, '.')\n *\n * // get just the first two parts, leave the rest intact\n * // Note: unlike str.split('.', 3), the 'rest' here will\n * // include the entire rest of the string.\n * // If you do `str.split('.', 3)`, then the last item in the\n * // returned array is truncated at the next delimiter\n * const [first, second, rest] = fastSplit(str, '.', 3)\n *\n * // If you need to transform it, say if it's an IPv4 address\n * // that you want to turn into numbers, you can do that by\n * // providing the onPart method, which will be slightly faster\n * // than getting an array and subsequently looping over it\n * // pass `-1` as the limit to give us all parts\n * const nums = fastSplit(str, '.', -1, (part, parts, index) => Number(s))\n * ```\n */\nexport function fastSplit<T = string>(\n str: string,\n delim: string,\n limit: number,\n onPart: (part: string, parts: NullToString<T>[], i: number) => T,\n): NullToString<T>[]\nexport function fastSplit(\n str: string,\n delim: string,\n limit?: number,\n): string[]\nexport function fastSplit<T = string>(\n str: string,\n delim: string,\n limit = -1,\n onPart?: (\n part: string,\n parts: NullToString<T>[],\n i: number,\n ) => T | undefined,\n): NullToString<T>[] {\n let i = 0\n let p = 0\n const l = delim.length\n const parts: NullToString<T>[] = []\n while (i !== -1) {\n i = str.indexOf(delim, p)\n const part =\n i === -1 || parts.length === limit - 1 ?\n str.substring(p)\n : str.substring(p, i)\n parts.push((onPart?.(part, parts, i) ?? part) as NullToString<T>)\n if (parts.length === limit) {\n // push the rest into the last part\n return parts\n }\n p = i + l\n }\n return parts\n}\n"]} |
| { | ||
| "type": "module" | ||
| } |
8
-11.11%13345
-2.28%7
-12.5%