@ls-stack/utils 1.3.0 → 1.4.0

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.
@@ -1,5 +1,6 @@
1
1
  declare function exhaustiveMatch<T extends string>(value: T): {
2
2
  with: <R>(pattern: { [K in T]: "_nxt" | "_never" | (() => R); }) => R;
3
+ withObject: <R_1>(pattern: Record<T, R_1>) => R_1;
3
4
  };
4
5
 
5
6
  export { exhaustiveMatch };
@@ -17,8 +17,12 @@ function exhaustiveMatch(value) {
17
17
  }
18
18
  throw new Error(`Exhaustive match failed: no match for ${value}`);
19
19
  }
20
+ function withObject(pattern) {
21
+ return pattern[value];
22
+ }
20
23
  return {
21
- with: matchWith
24
+ with: matchWith,
25
+ withObject
22
26
  };
23
27
  }
24
28
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/exhaustiveMatch.ts"],"sourcesContent":["export function exhaustiveMatch<T extends string>(value: T) {\n type Pattern<R> = {\n [K in T]: (() => R) | '_nxt' | '_never';\n };\n\n /**\n * The pattern can be:\n * - a function that returns the result\n * - '_nxt' to try the next pattern\n * - '_never' to indicate that this pattern should never be matched\n */\n function matchWith<R>(pattern: Pattern<R>): R {\n const result = pattern[value];\n\n if (typeof result === 'function') {\n return result();\n }\n\n if (result === '_nxt') {\n const keys = Object.keys(pattern);\n\n const nextIndex = keys.indexOf(value) + 1;\n\n for (let i = nextIndex; i < keys.length; i++) {\n const nextMatch = pattern[keys[i] as T];\n\n if (typeof nextMatch === 'function') {\n return nextMatch();\n }\n }\n }\n\n throw new Error(`Exhaustive match failed: no match for ${value}`);\n }\n\n return {\n with: matchWith,\n };\n}\n"],"mappings":";AAAO,SAAS,gBAAkC,OAAU;AAW1D,WAAS,UAAa,SAAwB;AAC5C,UAAM,SAAS,QAAQ,KAAK;AAE5B,QAAI,OAAO,WAAW,YAAY;AAChC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,WAAW,QAAQ;AACrB,YAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,YAAM,YAAY,KAAK,QAAQ,KAAK,IAAI;AAExC,eAAS,IAAI,WAAW,IAAI,KAAK,QAAQ,KAAK;AAC5C,cAAM,YAAY,QAAQ,KAAK,CAAC,CAAM;AAEtC,YAAI,OAAO,cAAc,YAAY;AACnC,iBAAO,UAAU;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAEA,UAAM,IAAI,MAAM,yCAAyC,KAAK,EAAE;AAAA,EAClE;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,EACR;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/exhaustiveMatch.ts"],"sourcesContent":["export function exhaustiveMatch<T extends string>(value: T) {\n type Pattern<R> = {\n [K in T]: (() => R) | '_nxt' | '_never';\n };\n\n /**\n * The pattern can be:\n * - a function that returns the result\n * - '_nxt' to try the next pattern\n * - '_never' to indicate that this pattern should never be matched\n */\n function matchWith<R>(pattern: Pattern<R>): R {\n const result = pattern[value];\n\n if (typeof result === 'function') {\n return result();\n }\n\n if (result === '_nxt') {\n const keys = Object.keys(pattern);\n\n const nextIndex = keys.indexOf(value) + 1;\n\n for (let i = nextIndex; i < keys.length; i++) {\n const nextMatch = pattern[keys[i] as T];\n\n if (typeof nextMatch === 'function') {\n return nextMatch();\n }\n }\n }\n\n throw new Error(`Exhaustive match failed: no match for ${value}`);\n }\n\n /** match with early evaluation of the values */\n function withObject<R>(pattern: Record<T, R>): R {\n return pattern[value];\n }\n\n return {\n with: matchWith,\n withObject,\n };\n}\n"],"mappings":";AAAO,SAAS,gBAAkC,OAAU;AAW1D,WAAS,UAAa,SAAwB;AAC5C,UAAM,SAAS,QAAQ,KAAK;AAE5B,QAAI,OAAO,WAAW,YAAY;AAChC,aAAO,OAAO;AAAA,IAChB;AAEA,QAAI,WAAW,QAAQ;AACrB,YAAM,OAAO,OAAO,KAAK,OAAO;AAEhC,YAAM,YAAY,KAAK,QAAQ,KAAK,IAAI;AAExC,eAAS,IAAI,WAAW,IAAI,KAAK,QAAQ,KAAK;AAC5C,cAAM,YAAY,QAAQ,KAAK,CAAC,CAAM;AAEtC,YAAI,OAAO,cAAc,YAAY;AACnC,iBAAO,UAAU;AAAA,QACnB;AAAA,MACF;AAAA,IACF;AAEA,UAAM,IAAI,MAAM,yCAAyC,KAAK,EAAE;AAAA,EAClE;AAGA,WAAS,WAAc,SAA0B;AAC/C,WAAO,QAAQ,KAAK;AAAA,EACtB;AAEA,SAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,EACF;AACF;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ls-stack/utils",
3
3
  "description": "Typescript utils",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "license": "MIT",
6
6
  "files": [
7
7
  "dist"