UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 1.62 kB
{"version":3,"file":"only.cjs","names":["purry"],"sources":["../src/only.ts"],"sourcesContent":["import type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport { purry } from \"./purry\";\n\ntype Only<T extends IterableContainer> = T extends\n | readonly [...unknown[], unknown, unknown]\n | readonly []\n | readonly [unknown, ...unknown[], unknown]\n | readonly [unknown, unknown, ...unknown[]]\n ? undefined\n : T extends readonly [unknown]\n ? T[number]\n : T[number] | undefined;\n\n/**\n * Returns the first and only element of `data`, or undefined otherwise.\n *\n * @param data - The target array.\n * @signature\n * only(data)\n * @example\n * only([]) // => undefined\n * only([1]) // => 1\n * only([1, 2]) // => undefined\n * @dataFirst\n * @category Array\n */\nexport function only<T extends IterableContainer>(data: T): Only<T>;\n\n/**\n * Returns the first and only element of `data`, or undefined otherwise.\n *\n * @signature\n * only()(data)\n * @example\n * pipe([], only()); // => undefined\n * pipe([1], only()); // => 1\n * pipe([1, 2], only()); // => undefined\n * @dataLast\n * @category Array\n */\nexport function only<T extends IterableContainer>(): (data: T) => Only<T>;\n\nexport function only(...args: readonly unknown[]): unknown {\n return purry(onlyImplementation, args);\n}\n\nconst onlyImplementation = <T>(data: readonly T[]): T | undefined =>\n data.length === 1 ? data[0] : undefined;\n"],"mappings":"kGA0CA,SAAgB,EAAK,GAAG,EAAmC,CACzD,OAAOA,EAAAA,MAAM,EAAoB,CAAI,CACvC,CAEA,MAAM,EAAyB,GAC7B,EAAK,SAAW,EAAI,EAAK,GAAK,IAAA"}