You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@vltpkg/fast-split

Package Overview
Dependencies
Maintainers
6
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vltpkg/fast-split - npm Package Compare versions

Comparing version
1.0.0-rc.18
to
1.0.0-rc.22
+22
-18
package.json
{
"name": "@vltpkg/fast-split",
"description": "A fast way to split small-to-medium sized strings by small string delimiters",
"version": "1.0.0-rc.18",
"version": "1.0.0-rc.22",
"repository": {

@@ -10,3 +10,6 @@ "type": "git",

},
"author": "vlt technology inc. <support@vlt.sh> (http://vlt.sh)",
"author": {
"name": "vlt technology inc.",
"email": "support@vlt.sh"
},
"devDependencies": {

@@ -24,4 +27,16 @@ "@eslint/js": "^9.39.1",

"engines": {
"node": ">=22.9.0"
"node": ">=22.22.0"
},
"scripts": {
"benchmark:split": "./test/fixtures/split-vs-fast-split.ts",
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"prepack": "tsc -p tsconfig.publish.json && ../../scripts/update-dist-exports.ts",
"snap": "tap",
"test": "tap",
"posttest": "tsc --noEmit",
"typecheck": "tsc --noEmit"
},
"tap": {

@@ -31,3 +46,3 @@ "extends": "../../tap-config.yaml"

"prettier": "../../.prettierrc.js",
"module": "./dist/index.js",
"module": "./src/index.ts",
"type": "module",

@@ -38,3 +53,3 @@ "exports": {

"import": {
"default": "./dist/index.js"
"default": "./src/index.ts"
}

@@ -45,14 +60,3 @@ }

"dist"
],
"scripts": {
"benchmark:split": "./test/fixtures/split-vs-fast-split.ts",
"format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
"format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"snap": "tap",
"test": "tap",
"posttest": "tsc --noEmit",
"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"]}