UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 4.78 kB
{"version":3,"file":"split.cjs","names":[],"sources":["../src/split.ts"],"sourcesContent":["import type {\n ArraySlice,\n IsFloat,\n NonNegative,\n Split as SplitBase,\n} from \"type-fest\";\n\n// We can use the type-fest's split **only** if all the params are literals.\n// For all other cases it would return the wrong type so we use\n// `Array.prototype.split`s return type instead.\ntype BuiltInReturnType = ReturnType<typeof String.prototype.split>;\n\ntype Split<\n S extends string,\n Separator extends string,\n N extends number | undefined = undefined,\n> = string extends S\n ? BuiltInReturnType\n : string extends Separator\n ? BuiltInReturnType\n : number extends N\n ? BuiltInReturnType\n : // TODO: We need a way to \"floor\" non-integer numbers, until then we return a lower fidelity type instead.\n IsFloat<N> extends true\n ? BuiltInReturnType\n : N extends number\n ? ArraySlice<SplitBase<S, Separator>, 0, NonNegative<N>>\n : SplitBase<S, Separator>;\n\n/**\n * Splits a string into an array of substrings using a separator pattern.\n *\n * This function is a wrapper around the built-in [`String.prototype.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)\n * method.\n *\n * @param data - The string to split.\n * @param separator - The pattern describing where each split should occur. Can\n * be a string, or a regular expression.\n * @param limit - A non-negative integer specifying a limit on the number of\n * substrings to be included in the array. If provided, splits the string at\n * each occurrence of the specified separator, but stops when limit entries have\n * been placed in the array. Any leftover text is not included in the array at\n * all. The array may contain fewer entries than limit if the end of the string\n * is reached before the limit is reached. If limit is 0, [] is returned.\n * @returns An array of strings, split at each point where the separator occurs\n * in the given string.\n * @signature\n * R.split(data, separator, limit);\n * @example\n * R.split(\"a,b,c\", \",\"); //=> [\"a\", \"b\", \"c\"]\n * R.split(\"a,b,c\", \",\", 2); //=> [\"a\", \"b\"]\n * R.split(\"a1b2c3d\", /\\d/u); //=> [\"a\", \"b\", \"c\", \"d\"]\n * @dataFirst\n * @category String\n */\nexport function split(\n data: string,\n separator: RegExp,\n limit?: number,\n): Array<string>;\nexport function split<\n S extends string,\n Separator extends string,\n N extends number | undefined = undefined,\n>(data: S, separator: Separator, limit?: N): Split<S, Separator, N>;\n\n/**\n * Splits a string into an array of substrings using a separator pattern.\n *\n * This function is a wrapper around the built-in [`String.prototype.split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)\n * method.\n *\n * @param separator - The pattern describing where each split should occur. Can\n * be a string, or a regular expression.\n * @param limit - A non-negative integer specifying a limit on the number of\n * substrings to be included in the array. If provided, splits the string at\n * each occurrence of the specified separator, but stops when limit entries have\n * been placed in the array. Any leftover text is not included in the array at\n * all. The array may contain fewer entries than limit if the end of the string\n * is reached before the limit is reached. If limit is 0, [] is returned.\n * @returns An array of strings, split at each point where the separator occurs\n * in the given string.\n * @signature\n * R.split(separator, limit)(data);\n * @example\n * R.pipe(\"a,b,c\", R.split(\",\")); //=> [\"a\", \"b\", \"c\"]\n * R.pipe(\"a,b,c\", R.split(\",\", 2)); //=> [\"a\", \"b\"]\n * R.pipe(\"a1b2c3d\", R.split(/\\d/u)); //=> [\"a\", \"b\", \"c\", \"d\"]\n * @dataLast\n * @category String\n */\nexport function split(\n separator: RegExp,\n limit?: number,\n): (data: string) => Array<string>;\nexport function split<\n S extends string,\n Separator extends string,\n N extends number | undefined = undefined,\n>(separator: Separator, limit?: N): (data: S) => Split<S, Separator, N>;\n\nexport function split(\n dataOrSeparator: RegExp | string,\n separatorOrLimit?: RegExp | number | string,\n limit?: number,\n): unknown {\n return typeof separatorOrLimit === \"number\" || separatorOrLimit === undefined\n ? // dataLast\n (data: string) => data.split(dataOrSeparator, separatorOrLimit)\n : // dataFirst\n (dataOrSeparator as string).split(separatorOrLimit, limit);\n}\n"],"mappings":"AAqGA,SAAgB,EACd,EACA,EACA,EACS,CACT,OAAO,OAAO,GAAqB,UAAY,IAAqB,IAAA,GAE/D,GAAiB,EAAK,MAAM,EAAiB,EAAiB,CAE9D,EAA2B,MAAM,EAAkB,EAAM"}