@oxog/utils 1.0.1 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +56 -43
  3. package/dist/chunk-25WW2FCA.js +34 -0
  4. package/dist/chunk-25WW2FCA.js.map +1 -0
  5. package/dist/chunk-DEVWOLUI.cjs +112 -0
  6. package/dist/chunk-DEVWOLUI.cjs.map +1 -0
  7. package/dist/chunk-IUDFQZIP.js +99 -0
  8. package/dist/chunk-IUDFQZIP.js.map +1 -0
  9. package/dist/chunk-PZ5AY32C.js +9 -0
  10. package/dist/chunk-PZ5AY32C.js.map +1 -0
  11. package/dist/chunk-Q7SFCCGT.cjs +11 -0
  12. package/dist/chunk-Q7SFCCGT.cjs.map +1 -0
  13. package/dist/chunk-SWG777R6.cjs +40 -0
  14. package/dist/chunk-SWG777R6.cjs.map +1 -0
  15. package/dist/index.cjs +210 -240
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +35 -11
  18. package/dist/index.d.ts +35 -11
  19. package/dist/index.js +156 -221
  20. package/dist/index.js.map +1 -1
  21. package/dist/plugins/async.cjs +90 -107
  22. package/dist/plugins/async.cjs.map +1 -1
  23. package/dist/plugins/async.d.cts +21 -7
  24. package/dist/plugins/async.d.ts +21 -7
  25. package/dist/plugins/async.js +89 -106
  26. package/dist/plugins/async.js.map +1 -1
  27. package/dist/plugins/deep.cjs +183 -59
  28. package/dist/plugins/deep.cjs.map +1 -1
  29. package/dist/plugins/deep.d.cts +27 -8
  30. package/dist/plugins/deep.d.ts +27 -8
  31. package/dist/plugins/deep.js +183 -59
  32. package/dist/plugins/deep.js.map +1 -1
  33. package/dist/plugins/guard.cjs +2 -0
  34. package/dist/plugins/guard.cjs.map +1 -1
  35. package/dist/plugins/guard.js +2 -0
  36. package/dist/plugins/guard.js.map +1 -1
  37. package/dist/plugins/transform.cjs +6 -3
  38. package/dist/plugins/transform.cjs.map +1 -1
  39. package/dist/plugins/transform.js +6 -3
  40. package/dist/plugins/transform.js.map +1 -1
  41. package/dist/{types-XmRC5wYO.d.cts → types-C2bIzOSX.d.cts} +52 -13
  42. package/dist/{types-XmRC5wYO.d.ts → types-C2bIzOSX.d.ts} +52 -13
  43. package/package.json +15 -10
@@ -1,23 +1,26 @@
1
+ import { assignOwn } from '../chunk-25WW2FCA.js';
2
+ import '../chunk-PZ5AY32C.js';
3
+
1
4
  // src/plugins/optional/transform.ts
2
5
  function mapKeys(obj, fn) {
3
6
  const result = {};
4
7
  for (const key of Object.keys(obj)) {
5
8
  const newKey = fn(key, obj[key]);
6
- result[newKey] = obj[key];
9
+ assignOwn(result, newKey, obj[key]);
7
10
  }
8
11
  return result;
9
12
  }
10
13
  function mapValues(obj, fn) {
11
14
  const result = {};
12
15
  for (const key of Object.keys(obj)) {
13
- result[key] = fn(obj[key], key);
16
+ assignOwn(result, key, fn(obj[key], key));
14
17
  }
15
18
  return result;
16
19
  }
17
20
  function invert(obj) {
18
21
  const result = {};
19
22
  for (const key of Object.keys(obj)) {
20
- result[String(obj[key])] = key;
23
+ assignOwn(result, String(obj[key]), key);
21
24
  }
22
25
  return result;
23
26
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/plugins/optional/transform.ts"],"names":[],"mappings":";AA6BO,SAAS,OAAA,CACd,KACA,EAAA,EACuB;AACvB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAM,MAAA,GAAS,EAAA,CAAG,GAAA,EAAK,GAAA,CAAI,GAAG,CAAC,CAAA;AAC/B,IAAA,MAAA,CAAO,MAAM,CAAA,GAAI,GAAA,CAAI,GAAG,CAAA;AAAA,EAC1B;AAEA,EAAA,OAAO,MAAA;AACT;AA+BO,SAAS,SAAA,CACd,KACA,EAAA,EACoB;AACpB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAA,CAAO,GAAG,CAAA,GAAI,EAAA,CAAG,GAAA,CAAI,GAAG,GAAG,GAAG,CAAA;AAAA,EAChC;AAEA,EAAA,OAAO,MAAA;AACT;AAmCO,SAAS,OAAyB,GAAA,EAAiC;AACxE,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAA,CAAO,MAAA,CAAO,GAAA,CAAI,GAAG,CAAC,CAAC,CAAA,GAAI,GAAA;AAAA,EAC7B;AAEA,EAAA,OAAO,MAAA;AACT;AAiCO,SAAS,KAAwC,EAAA,EAAU;AAChE,EAAA,QAAQ,IAAI,IAAA,KAAwB;AAClC,IAAA,OAAO,EAAA,CAAG,GAAG,IAAA,CAAK,OAAA,EAAS,CAAA;AAAA,EAC7B,CAAA;AACF;AA+CO,SAAS,WAAc,GAAA,EAAgD;AAC5E,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,YAAY,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAClD,CAAA;AACF;AA0DO,SAAS,QAAW,GAAA,EAAgD;AACzE,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,OAAO,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAC7C,CAAA;AACF","file":"transform.js","sourcesContent":["/**\n * Transform object keys with a function.\n *\n * Creates a new object with keys transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform keys\n * @returns New object with transformed keys\n *\n * @example Basic usage\n * ```typescript\n * mapKeys({ a: 1, b: 2 }, (key) => key.toUpperCase());\n * // { A: 1, B: 2 }\n * ```\n *\n * @example Adding prefix\n * ```typescript\n * mapKeys({ name: 'Alice', age: 30 }, (key) => `user_${key}`);\n * // { user_name: 'Alice', user_age: 30 }\n * ```\n *\n * @example Snake case conversion\n * ```typescript\n * mapKeys({ firstName: 'Alice', lastName: 'Smith' }, (key) => snakeCase(key));\n * // { first_name: 'Alice', last_name: 'Smith' }\n * ```\n *\n * @see {@link mapValues} for transforming values\n */\nexport function mapKeys<T extends object, K extends PropertyKey>(\n obj: T,\n fn: (key: keyof T, value: T[keyof T]) => K\n): Record<K, T[keyof T]> {\n const result = {} as Record<K, T[keyof T]>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n const newKey = fn(key, obj[key]);\n result[newKey] = obj[key];\n }\n\n return result;\n}\n\n/**\n * Transform object values with a function.\n *\n * Creates a new object with values transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform values\n * @returns New object with transformed values\n *\n * @example Basic usage\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val) => val * 2);\n * // { a: 2, b: 4 }\n * ```\n *\n * @example String transformation\n * ```typescript\n * mapValues({ name: 'alice', city: 'ny' }, (val) => val.toUpperCase());\n * // { name: 'ALICE', city: 'NY' }\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val, key) => `${key}:${val}`);\n * // { a: 'a:1', b: 'b:2' }\n * ```\n *\n * @see {@link mapKeys} for transforming keys\n */\nexport function mapValues<T extends object, R>(\n obj: T,\n fn: (value: T[keyof T], key: keyof T) => R\n): Record<keyof T, R> {\n const result = {} as Record<keyof T, R>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n result[key] = fn(obj[key], key);\n }\n\n return result;\n}\n\n/**\n * Swap keys and values of an object.\n *\n * Creates a new object where keys become values and values become keys.\n * If values are not unique, later values will overwrite earlier ones.\n *\n * @param obj - Source object\n * @returns Inverted object\n *\n * @example Basic usage\n * ```typescript\n * invert({ a: '1', b: '2', c: '3' });\n * // { '1': 'a', '2': 'b', '3': 'c' }\n * ```\n *\n * @example Number values\n * ```typescript\n * invert({ a: 1, b: 2, c: 3 });\n * // { 1: 'a', 2: 'b', 3: 'c' }\n * ```\n *\n * @example Duplicate values\n * ```typescript\n * invert({ a: 'x', b: 'x', c: 'y' });\n * // { 'x': 'b', 'y': 'c' } (last 'x' wins)\n * ```\n *\n * @example Non-string keys become strings\n * ```typescript\n * invert({ 1: 'a', 2: 'b' });\n * // { 'a': '1', 'b': '2' }\n * ```\n */\nexport function invert<T extends object>(obj: T): Record<string, keyof T> {\n const result = {} as Record<string, keyof T>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n result[String(obj[key])] = key;\n }\n\n return result;\n}\n\n/**\n * Flip function argument order.\n *\n * Creates a new function with reversed argument order.\n *\n * @param fn - Function to flip\n * @returns New function with flipped arguments\n *\n * @example Basic usage\n * ```typescript\n * const divide = (a: number, b: number) => a / b;\n * const flippedDivide = flip(divide);\n * divide(10, 2); // 5\n * flippedDivide(10, 2); // 0.2 (same as divide(2, 10))\n * ```\n *\n * @example With map\n * ```typescript\n * const subtract = (a: number, b: number) => a - b;\n * const subtractFrom10 = flip(subtract)(10);\n * [1, 2, 3, 4, 5].map(subtractFrom10);\n * // [9, 8, 7, 6, 5]\n * ```\n *\n * @example With currying\n * ```typescript\n * const append = (prefix: string, suffix: string) => prefix + suffix;\n * const appendToHello = flip(append)('hello');\n * appendToHello(' world'); // ' worldhello'\n * ```\n */\nexport function flip<T extends (...args: any[]) => any>(fn: T): T {\n return ((...args: Parameters<T>) => {\n return fn(...args.reverse());\n }) as T;\n}\n\n/**\n * Compose functions right-to-left.\n *\n * Creates a function that applies functions from right to left.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const process = compose(toString, multiply, add);\n * process(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);\n * const exclaim = (s: string) => s + '!';\n * const greet = compose(exclaim, capitalize);\n *\n * greet('hello');\n * // 'Hello!'\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * compose(f3, f2, f1)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link pipe} for left-to-right composition\n */\nexport function compose<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduceRight((acc, fn) => fn(acc), arg);\n };\n}\n\n/**\n * Compose functions left-to-right.\n *\n * Creates a function that applies functions from left to right.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const pipeline = pipe(add, multiply, toString);\n * pipeline(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Data transformation\n * ```typescript\n * const toNumber = (s: string) => parseInt(s);\n * const double = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const transform = pipe(toNumber, double, toString);\n * transform('5');\n * // '10'\n * ```\n *\n * @example API data processing\n * ```typescript\n * const fetchData = () => ({ items: [{ price: 10 }, { price: 20 }] });\n * const getItems = (data) => data.items;\n * const sumPrices = (items) => items.reduce((sum, item) => sum + item.price, 0);\n *\n * const getTotal = pipe(fetchData, getItems, sumPrices);\n * await getTotal(); // 30\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * pipe(f1, f2, f3)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link compose} for right-to-left composition\n */\nexport function pipe<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduce((acc, fn) => fn(acc), arg);\n };\n}\n"]}
1
+ {"version":3,"sources":["../../src/plugins/optional/transform.ts"],"names":[],"mappings":";;;;AA+BO,SAAS,OAAA,CACd,KACA,EAAA,EACuB;AACvB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAM,MAAA,GAAS,EAAA,CAAG,GAAA,EAAK,GAAA,CAAI,GAAG,CAAC,CAAA;AAC/B,IAAA,SAAA,CAAU,MAAA,EAAQ,MAAA,EAAQ,GAAA,CAAI,GAAG,CAAC,CAAA;AAAA,EACpC;AAEA,EAAA,OAAO,MAAA;AACT;AA+BO,SAAS,SAAA,CACd,KACA,EAAA,EACoB;AACpB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,SAAA,CAAU,QAAQ,GAAA,EAAK,EAAA,CAAG,IAAI,GAAG,CAAA,EAAG,GAAG,CAAC,CAAA;AAAA,EAC1C;AAEA,EAAA,OAAO,MAAA;AACT;AAmCO,SAAS,OAAyB,GAAA,EAAiC;AACxE,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,SAAA,CAAU,QAAQ,MAAA,CAAO,GAAA,CAAI,GAAG,CAAC,GAAG,GAAG,CAAA;AAAA,EACzC;AAEA,EAAA,OAAO,MAAA;AACT;AAiCO,SAAS,KAAwC,EAAA,EAAU;AAChE,EAAA,QAAQ,IAAI,IAAA,KAAwB;AAClC,IAAA,OAAO,EAAA,CAAG,GAAG,IAAA,CAAK,OAAA,EAAS,CAAA;AAAA,EAC7B,CAAA;AACF;AA+CO,SAAS,WAAc,GAAA,EAAgD;AAC5E,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,YAAY,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAClD,CAAA;AACF;AA0DO,SAAS,QAAW,GAAA,EAAgD;AACzE,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,OAAO,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAC7C,CAAA;AACF","file":"transform.js","sourcesContent":["import { assignOwn } from '../../internal.js';\n\n/**\n * Transform object keys with a function.\n *\n * Creates a new object with keys transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform keys\n * @returns New object with transformed keys\n *\n * @example Basic usage\n * ```typescript\n * mapKeys({ a: 1, b: 2 }, (key) => key.toUpperCase());\n * // { A: 1, B: 2 }\n * ```\n *\n * @example Adding prefix\n * ```typescript\n * mapKeys({ name: 'Alice', age: 30 }, (key) => `user_${key}`);\n * // { user_name: 'Alice', user_age: 30 }\n * ```\n *\n * @example Snake case conversion\n * ```typescript\n * mapKeys({ firstName: 'Alice', lastName: 'Smith' }, (key) => snakeCase(key));\n * // { first_name: 'Alice', last_name: 'Smith' }\n * ```\n *\n * @see {@link mapValues} for transforming values\n */\nexport function mapKeys<T extends object, K extends PropertyKey>(\n obj: T,\n fn: (key: keyof T, value: T[keyof T]) => K\n): Record<K, T[keyof T]> {\n const result = {} as Record<K, T[keyof T]>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n const newKey = fn(key, obj[key]);\n assignOwn(result, newKey, obj[key]);\n }\n\n return result;\n}\n\n/**\n * Transform object values with a function.\n *\n * Creates a new object with values transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform values\n * @returns New object with transformed values\n *\n * @example Basic usage\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val) => val * 2);\n * // { a: 2, b: 4 }\n * ```\n *\n * @example String transformation\n * ```typescript\n * mapValues({ name: 'alice', city: 'ny' }, (val) => val.toUpperCase());\n * // { name: 'ALICE', city: 'NY' }\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val, key) => `${key}:${val}`);\n * // { a: 'a:1', b: 'b:2' }\n * ```\n *\n * @see {@link mapKeys} for transforming keys\n */\nexport function mapValues<T extends object, R>(\n obj: T,\n fn: (value: T[keyof T], key: keyof T) => R\n): Record<keyof T, R> {\n const result = {} as Record<keyof T, R>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n assignOwn(result, key, fn(obj[key], key));\n }\n\n return result;\n}\n\n/**\n * Swap keys and values of an object.\n *\n * Creates a new object where keys become values and values become keys.\n * If values are not unique, later values will overwrite earlier ones.\n *\n * @param obj - Source object\n * @returns Inverted object\n *\n * @example Basic usage\n * ```typescript\n * invert({ a: '1', b: '2', c: '3' });\n * // { '1': 'a', '2': 'b', '3': 'c' }\n * ```\n *\n * @example Number values\n * ```typescript\n * invert({ a: 1, b: 2, c: 3 });\n * // { 1: 'a', 2: 'b', 3: 'c' }\n * ```\n *\n * @example Duplicate values\n * ```typescript\n * invert({ a: 'x', b: 'x', c: 'y' });\n * // { 'x': 'b', 'y': 'c' } (last 'x' wins)\n * ```\n *\n * @example Non-string keys become strings\n * ```typescript\n * invert({ 1: 'a', 2: 'b' });\n * // { 'a': '1', 'b': '2' }\n * ```\n */\nexport function invert<T extends object>(obj: T): Record<string, keyof T> {\n const result = {} as Record<string, keyof T>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n assignOwn(result, String(obj[key]), key);\n }\n\n return result;\n}\n\n/**\n * Flip function argument order.\n *\n * Creates a new function with reversed argument order.\n *\n * @param fn - Function to flip\n * @returns New function with flipped arguments\n *\n * @example Basic usage\n * ```typescript\n * const divide = (a: number, b: number) => a / b;\n * const flippedDivide = flip(divide);\n * divide(10, 2); // 5\n * flippedDivide(10, 2); // 0.2 (same as divide(2, 10))\n * ```\n *\n * @example With map\n * ```typescript\n * const subtract = (a: number, b: number) => a - b;\n * const subtractFrom10 = flip(subtract)(10);\n * [1, 2, 3, 4, 5].map(subtractFrom10);\n * // [9, 8, 7, 6, 5]\n * ```\n *\n * @example With currying\n * ```typescript\n * const append = (prefix: string, suffix: string) => prefix + suffix;\n * const appendToHello = flip(append)('hello');\n * appendToHello(' world'); // ' worldhello'\n * ```\n */\nexport function flip<T extends (...args: any[]) => any>(fn: T): T {\n return ((...args: Parameters<T>) => {\n return fn(...args.reverse());\n }) as T;\n}\n\n/**\n * Compose functions right-to-left.\n *\n * Creates a function that applies functions from right to left.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const process = compose(toString, multiply, add);\n * process(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);\n * const exclaim = (s: string) => s + '!';\n * const greet = compose(exclaim, capitalize);\n *\n * greet('hello');\n * // 'Hello!'\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * compose(f3, f2, f1)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link pipe} for left-to-right composition\n */\nexport function compose<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduceRight((acc, fn) => fn(acc), arg);\n };\n}\n\n/**\n * Compose functions left-to-right.\n *\n * Creates a function that applies functions from left to right.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const pipeline = pipe(add, multiply, toString);\n * pipeline(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Data transformation\n * ```typescript\n * const toNumber = (s: string) => parseInt(s);\n * const double = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const transform = pipe(toNumber, double, toString);\n * transform('5');\n * // '10'\n * ```\n *\n * @example API data processing\n * ```typescript\n * const fetchData = () => ({ items: [{ price: 10 }, { price: 20 }] });\n * const getItems = (data) => data.items;\n * const sumPrices = (items) => items.reduce((sum, item) => sum + item.price, 0);\n *\n * const getTotal = pipe(fetchData, getItems, sumPrices);\n * await getTotal(); // 30\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * pipe(f1, f2, f3)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link compose} for right-to-left composition\n */\nexport function pipe<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduce((acc, fn) => fn(acc), arg);\n };\n}\n"]}
@@ -14,21 +14,23 @@ interface Plugin<TContext = unknown> {
14
14
  * Called when plugin is registered.
15
15
  * @param kernel - The kernel instance
16
16
  */
17
- install: (kernel: any) => void;
17
+ install(kernel: any): void;
18
18
  /**
19
- * Called after all plugins are installed.
19
+ * Called by `kernel.init()` in dependency order. May return a promise,
20
+ * which is awaited (its value is ignored).
20
21
  * @param context - Shared context object
21
22
  */
22
- onInit?: (context: TContext) => void | Promise<void>;
23
+ onInit?(context: TContext): unknown;
23
24
  /**
24
- * Called when plugin is unregistered.
25
+ * Called when the plugin is unregistered or the kernel is destroyed.
26
+ * May return a promise, which is awaited (its value is ignored).
25
27
  */
26
- onDestroy?: () => void | Promise<void>;
28
+ onDestroy?(): unknown;
27
29
  /**
28
30
  * Called on error in this plugin.
29
31
  * @param error - The error that occurred
30
32
  */
31
- onError?: (error: Error) => void;
33
+ onError?(error: Error): void;
32
34
  }
33
35
  /**
34
36
  * Options for debounce() function.
@@ -50,12 +52,28 @@ interface DebounceOptions {
50
52
  */
51
53
  maxWait?: number;
52
54
  }
55
+ /**
56
+ * Options for throttle() function.
57
+ */
58
+ interface ThrottleOptions {
59
+ /**
60
+ * Invoke on leading edge of timeout.
61
+ * @default true
62
+ */
63
+ leading?: boolean;
64
+ /**
65
+ * Invoke on trailing edge of timeout.
66
+ * @default true
67
+ */
68
+ trailing?: boolean;
69
+ }
53
70
  /**
54
71
  * Options for retry() function.
55
72
  */
56
73
  interface RetryOptions<T = any> {
57
74
  /**
58
- * Number of retry attempts.
75
+ * Total number of attempts, including the first call
76
+ * (`attempts: 3` means at most 2 retries).
59
77
  * @default 3
60
78
  */
61
79
  attempts?: number;
@@ -82,16 +100,37 @@ interface RetryOptions<T = any> {
82
100
  /**
83
101
  * Result object for diff() function.
84
102
  */
103
+ /**
104
+ * A changed value reported by diff(): the old and the new value.
105
+ */
106
+ interface DiffChange {
107
+ from: any;
108
+ to: any;
109
+ }
110
+ /**
111
+ * Map of changes reported by diff(). A nested plain object or array whose only
112
+ * differences are changed values is reported as a nested DiffChangeMap instead
113
+ * of a DiffChange, so each entry must be narrowed before reading `from`/`to`:
114
+ *
115
+ * ```typescript
116
+ * const entry = diff(a, b).changed.user;
117
+ * if ('from' in entry && 'to' in entry) {
118
+ * // leaf change: entry.from, entry.to
119
+ * } else {
120
+ * // nested map: entry.age, ...
121
+ * }
122
+ * ```
123
+ */
124
+ interface DiffChangeMap {
125
+ [key: string]: DiffChange | DiffChangeMap;
126
+ }
85
127
  interface DiffResult {
86
- /** Values that changed */
87
- changed: Record<string, {
88
- from: any;
89
- to: any;
90
- }>;
128
+ /** Values that changed; nested value-only changes are nested maps */
129
+ changed: DiffChangeMap;
91
130
  /** Values that were added */
92
131
  added: Record<string, any>;
93
132
  /** Values that were removed */
94
133
  removed: Record<string, any>;
95
134
  }
96
135
 
97
- export type { DebounceOptions as D, Plugin as P, RetryOptions as R, DiffResult as a };
136
+ export type { DebounceOptions as D, Plugin as P, RetryOptions as R, ThrottleOptions as T, DiffResult as a, DiffChange as b, DiffChangeMap as c };
@@ -14,21 +14,23 @@ interface Plugin<TContext = unknown> {
14
14
  * Called when plugin is registered.
15
15
  * @param kernel - The kernel instance
16
16
  */
17
- install: (kernel: any) => void;
17
+ install(kernel: any): void;
18
18
  /**
19
- * Called after all plugins are installed.
19
+ * Called by `kernel.init()` in dependency order. May return a promise,
20
+ * which is awaited (its value is ignored).
20
21
  * @param context - Shared context object
21
22
  */
22
- onInit?: (context: TContext) => void | Promise<void>;
23
+ onInit?(context: TContext): unknown;
23
24
  /**
24
- * Called when plugin is unregistered.
25
+ * Called when the plugin is unregistered or the kernel is destroyed.
26
+ * May return a promise, which is awaited (its value is ignored).
25
27
  */
26
- onDestroy?: () => void | Promise<void>;
28
+ onDestroy?(): unknown;
27
29
  /**
28
30
  * Called on error in this plugin.
29
31
  * @param error - The error that occurred
30
32
  */
31
- onError?: (error: Error) => void;
33
+ onError?(error: Error): void;
32
34
  }
33
35
  /**
34
36
  * Options for debounce() function.
@@ -50,12 +52,28 @@ interface DebounceOptions {
50
52
  */
51
53
  maxWait?: number;
52
54
  }
55
+ /**
56
+ * Options for throttle() function.
57
+ */
58
+ interface ThrottleOptions {
59
+ /**
60
+ * Invoke on leading edge of timeout.
61
+ * @default true
62
+ */
63
+ leading?: boolean;
64
+ /**
65
+ * Invoke on trailing edge of timeout.
66
+ * @default true
67
+ */
68
+ trailing?: boolean;
69
+ }
53
70
  /**
54
71
  * Options for retry() function.
55
72
  */
56
73
  interface RetryOptions<T = any> {
57
74
  /**
58
- * Number of retry attempts.
75
+ * Total number of attempts, including the first call
76
+ * (`attempts: 3` means at most 2 retries).
59
77
  * @default 3
60
78
  */
61
79
  attempts?: number;
@@ -82,16 +100,37 @@ interface RetryOptions<T = any> {
82
100
  /**
83
101
  * Result object for diff() function.
84
102
  */
103
+ /**
104
+ * A changed value reported by diff(): the old and the new value.
105
+ */
106
+ interface DiffChange {
107
+ from: any;
108
+ to: any;
109
+ }
110
+ /**
111
+ * Map of changes reported by diff(). A nested plain object or array whose only
112
+ * differences are changed values is reported as a nested DiffChangeMap instead
113
+ * of a DiffChange, so each entry must be narrowed before reading `from`/`to`:
114
+ *
115
+ * ```typescript
116
+ * const entry = diff(a, b).changed.user;
117
+ * if ('from' in entry && 'to' in entry) {
118
+ * // leaf change: entry.from, entry.to
119
+ * } else {
120
+ * // nested map: entry.age, ...
121
+ * }
122
+ * ```
123
+ */
124
+ interface DiffChangeMap {
125
+ [key: string]: DiffChange | DiffChangeMap;
126
+ }
85
127
  interface DiffResult {
86
- /** Values that changed */
87
- changed: Record<string, {
88
- from: any;
89
- to: any;
90
- }>;
128
+ /** Values that changed; nested value-only changes are nested maps */
129
+ changed: DiffChangeMap;
91
130
  /** Values that were added */
92
131
  added: Record<string, any>;
93
132
  /** Values that were removed */
94
133
  removed: Record<string, any>;
95
134
  }
96
135
 
97
- export type { DebounceOptions as D, Plugin as P, RetryOptions as R, DiffResult as a };
136
+ export type { DebounceOptions as D, Plugin as P, RetryOptions as R, ThrottleOptions as T, DiffResult as a, DiffChange as b, DiffChangeMap as c };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oxog/utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Zero-dependency, type-safe utility functions for Node.js backends",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -56,7 +56,8 @@
56
56
  "types": "./dist/plugins/transform.d.cts",
57
57
  "default": "./dist/plugins/transform.cjs"
58
58
  }
59
- }
59
+ },
60
+ "./package.json": "./package.json"
60
61
  },
61
62
  "files": [
62
63
  "dist",
@@ -69,11 +70,13 @@
69
70
  "build": "tsup",
70
71
  "test": "jest",
71
72
  "test:watch": "jest --watch",
72
- "test:coverage": "c8 --reporter=text --reporter=lcov --reporter=json jest",
73
- "lint": "eslint src/",
73
+ "test:coverage": "jest --coverage",
74
+ "test:package": "node scripts/check-package.mjs",
75
+ "lint": "eslint src __tests__ scripts tests --max-warnings=0",
74
76
  "format": "prettier --write .",
75
- "typecheck": "tsc --noEmit",
76
- "prepublishOnly": "npm run build && npm run test:coverage"
77
+ "typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
78
+ "check": "npm run lint && npm run typecheck && npm run test:coverage",
79
+ "prepublishOnly": "npm run check && npm run build && npm run test:package"
77
80
  },
78
81
  "keywords": [
79
82
  "utils",
@@ -91,25 +94,27 @@
91
94
  "license": "MIT",
92
95
  "repository": {
93
96
  "type": "git",
94
- "url": "git+https://github.com/ersinkoc/utils.git"
97
+ "url": "git+https://github.com/ersinkoc/Utils.git"
95
98
  },
96
99
  "bugs": {
97
- "url": "https://github.com/ersinkoc/utils/issues"
100
+ "url": "https://github.com/ersinkoc/Utils/issues"
98
101
  },
99
102
  "homepage": "https://utils.oxog.dev",
100
103
  "engines": {
101
104
  "node": ">=18"
102
105
  },
103
106
  "devDependencies": {
107
+ "@eslint/js": "^9.39.5",
104
108
  "@jest/globals": "^30.2.0",
105
109
  "@types/jest": "^30.0.0",
106
110
  "@types/node": "^20.0.0",
107
- "c8": "^10.1.3",
108
111
  "eslint": "^9.0.0",
112
+ "globals": "^17.14.0",
109
113
  "jest": "^30.2.0",
110
114
  "prettier": "^3.0.0",
111
115
  "ts-jest": "^29.4.6",
112
116
  "tsup": "^8.0.0",
113
- "typescript": "^5.0.0"
117
+ "typescript": "^5.0.0",
118
+ "typescript-eslint": "^8.74.0"
114
119
  }
115
120
  }