@scalar/helpers 0.1.3 → 0.2.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @scalar/helpers
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#7489](https://github.com/scalar/scalar/pull/7489) [`21aa62e`](https://github.com/scalar/scalar/commit/21aa62e2ebdd262cb5aa53658c3b659736660722) Thanks [@amritk](https://github.com/amritk)! - feat: added new helpers for building client v2 requests
8
+
9
+ ## 0.2.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#7477](https://github.com/scalar/scalar/pull/7477) [`9ec8adf`](https://github.com/scalar/scalar/commit/9ec8adfea017333dee5bc3949104232f7dc57f4a) Thanks [@DemonHa](https://github.com/DemonHa)! - fix: correctly sort documents on the workspace
14
+
3
15
  ## 0.1.3
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Immutably sorts an array based on a given custom order, as specified by a separate list of identifiers.
3
+ *
4
+ * Note: Make sure that the identifier is unique for each item in the array.
5
+ * If the identifier is not unique, only the last occurrence of the identifier will be sorted.
6
+ * Any other elements will be overridden by the last occurrence of the identifier.
7
+ *
8
+ * This function efficiently arranges elements of an input array so that items whose IDs (obtained via the `getId` callback)
9
+ * match the order array will appear first, strictly in the order specified. Items not found in the order array will be
10
+ * appended after, maintaining their original order.
11
+ *
12
+ * This is a flexible utility: the identifier can be extracted from any data structure via the `getId` callback,
13
+ * and any primitive (string, number, symbol, etc.) can serve as the identifier type.
14
+ * Sorting is O(n) with respect to the array size, making it performant even for large arrays.
15
+ *
16
+ * @template T - The type of array elements.
17
+ * @template N - The type of values in the order array and the identifier returned by the getId callback.
18
+ * @param arr - The array to be sorted.
19
+ * @param order - Array specifying the desired sequence (contains identifiers returned by getId).
20
+ * @param getId - A callback to extract the unique identifier from each array element.
21
+ * @returns A new array sorted according to the order provided, with unmatched elements following.
22
+ *
23
+ * @example
24
+ * // Sorting an array of objects:
25
+ * const items = [
26
+ * { id: 'a', name: 'Alpha' },
27
+ * { id: 'b', name: 'Bravo' },
28
+ * { id: 'c', name: 'Charlie' }
29
+ * ];
30
+ * const order = ['c', 'a'];
31
+ * const sorted = sortByOrder(items, order, item => item.id);
32
+ * // Result:
33
+ * // [
34
+ * // { id: 'c', name: 'Charlie' },
35
+ * // { id: 'a', name: 'Alpha' },
36
+ * // { id: 'b', name: 'Bravo' }
37
+ * // ]
38
+ *
39
+ * @example
40
+ * // Sorting an array of primitive values:
41
+ * const input = ['a', 'b', 'c', 'd'];
42
+ * const order = ['c', 'a'];
43
+ * sortByOrder(input, order, item => item);
44
+ * // Result: ['c', 'a', 'b', 'd']
45
+ */
46
+ export declare function sortByOrder<T, N>(arr: T[], order: N[], getId: (item: T) => N): T[];
47
+ //# sourceMappingURL=sort-by-order.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort-by-order.d.ts","sourceRoot":"","sources":["../../src/array/sort-by-order.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAkBlF"}
@@ -0,0 +1,19 @@
1
+ function sortByOrder(arr, order, getId) {
2
+ const orderMap = /* @__PURE__ */ new Map();
3
+ order.forEach((e, idx) => orderMap.set(e, idx));
4
+ const sorted = [];
5
+ const untagged = [];
6
+ arr.forEach((e) => {
7
+ const sortedIdx = orderMap.get(getId(e));
8
+ if (sortedIdx === void 0) {
9
+ untagged.push(e);
10
+ return;
11
+ }
12
+ sorted[sortedIdx] = e;
13
+ });
14
+ return [...sorted.filter((it) => it !== void 0), ...untagged];
15
+ }
16
+ export {
17
+ sortByOrder
18
+ };
19
+ //# sourceMappingURL=sort-by-order.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/array/sort-by-order.ts"],
4
+ "sourcesContent": ["/**\n * Immutably sorts an array based on a given custom order, as specified by a separate list of identifiers.\n *\n * Note: Make sure that the identifier is unique for each item in the array.\n * If the identifier is not unique, only the last occurrence of the identifier will be sorted.\n * Any other elements will be overridden by the last occurrence of the identifier.\n *\n * This function efficiently arranges elements of an input array so that items whose IDs (obtained via the `getId` callback)\n * match the order array will appear first, strictly in the order specified. Items not found in the order array will be\n * appended after, maintaining their original order.\n *\n * This is a flexible utility: the identifier can be extracted from any data structure via the `getId` callback,\n * and any primitive (string, number, symbol, etc.) can serve as the identifier type.\n * Sorting is O(n) with respect to the array size, making it performant even for large arrays.\n *\n * @template T - The type of array elements.\n * @template N - The type of values in the order array and the identifier returned by the getId callback.\n * @param arr - The array to be sorted.\n * @param order - Array specifying the desired sequence (contains identifiers returned by getId).\n * @param getId - A callback to extract the unique identifier from each array element.\n * @returns A new array sorted according to the order provided, with unmatched elements following.\n *\n * @example\n * // Sorting an array of objects:\n * const items = [\n * { id: 'a', name: 'Alpha' },\n * { id: 'b', name: 'Bravo' },\n * { id: 'c', name: 'Charlie' }\n * ];\n * const order = ['c', 'a'];\n * const sorted = sortByOrder(items, order, item => item.id);\n * // Result:\n * // [\n * // { id: 'c', name: 'Charlie' },\n * // { id: 'a', name: 'Alpha' },\n * // { id: 'b', name: 'Bravo' }\n * // ]\n *\n * @example\n * // Sorting an array of primitive values:\n * const input = ['a', 'b', 'c', 'd'];\n * const order = ['c', 'a'];\n * sortByOrder(input, order, item => item);\n * // Result: ['c', 'a', 'b', 'd']\n */\nexport function sortByOrder<T, N>(arr: T[], order: N[], getId: (item: T) => N): T[] {\n // Map the order to keep a single lookup table\n const orderMap = new Map<N, number>()\n order.forEach((e, idx) => orderMap.set(e, idx))\n\n const sorted: T[] = []\n const untagged: T[] = []\n\n arr.forEach((e) => {\n const sortedIdx = orderMap.get(getId(e))\n if (sortedIdx === undefined) {\n untagged.push(e)\n return\n }\n sorted[sortedIdx] = e\n })\n\n return [...sorted.filter((it) => it !== undefined), ...untagged]\n}\n"],
5
+ "mappings": "AA6CO,SAAS,YAAkB,KAAU,OAAY,OAA4B;AAElF,QAAM,WAAW,oBAAI,IAAe;AACpC,QAAM,QAAQ,CAAC,GAAG,QAAQ,SAAS,IAAI,GAAG,GAAG,CAAC;AAE9C,QAAM,SAAc,CAAC;AACrB,QAAM,WAAgB,CAAC;AAEvB,MAAI,QAAQ,CAAC,MAAM;AACjB,UAAM,YAAY,SAAS,IAAI,MAAM,CAAC,CAAC;AACvC,QAAI,cAAc,QAAW;AAC3B,eAAS,KAAK,CAAC;AACf;AAAA,IACF;AACA,WAAO,SAAS,IAAI;AAAA,EACtB,CAAC;AAED,SAAO,CAAC,GAAG,OAAO,OAAO,CAAC,OAAO,OAAO,MAAS,GAAG,GAAG,QAAQ;AACjE;",
6
+ "names": []
7
+ }
@@ -2,4 +2,8 @@
2
2
  * This function takes a string and replace {variables} with given values.
3
3
  */
4
4
  export declare function replaceVariables(value: string, variablesOrCallback: Record<string, string | number> | ((match: string) => string)): string;
5
+ /** Replace {path} variables with their values */
6
+ export declare const replacePathVariables: (path: string, variables?: Record<string, string>) => string;
7
+ /** Replace {{env}} variables with their values */
8
+ export declare const replaceEnvVariables: (path: string, variables?: Record<string, string>) => string;
5
9
  //# sourceMappingURL=replace-variables.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"replace-variables.d.ts","sourceRoot":"","sources":["../../src/regex/replace-variables.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,UAenF"}
1
+ {"version":3,"file":"replace-variables.d.ts","sourceRoot":"","sources":["../../src/regex/replace-variables.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,MAAM,EACb,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,UAenF;AAED,iDAAiD;AACjD,eAAO,MAAM,oBAAoB,GAAI,MAAM,MAAM,EAAE,YAAW,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,WACtB,CAAA;AAEnE,kDAAkD;AAClD,eAAO,MAAM,mBAAmB,GAAI,MAAM,MAAM,EAAE,YAAW,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,WAChB,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { REGEX } from "../regex/regex-helpers.js";
1
2
  function replaceVariables(value, variablesOrCallback) {
2
3
  const doubleCurlyBrackets = /{{\s*([\w.-]+)\s*}}/g;
3
4
  const singleCurlyBrackets = /{\s*([\w.-]+)\s*}/g;
@@ -9,7 +10,11 @@ function replaceVariables(value, variablesOrCallback) {
9
10
  };
10
11
  return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback);
11
12
  }
13
+ const replacePathVariables = (path, variables = {}) => path.replace(REGEX.PATH, (match, key) => variables[key] ?? match);
14
+ const replaceEnvVariables = (path, variables = {}) => path.replace(REGEX.VARIABLES, (match, key) => variables[key] ?? match);
12
15
  export {
16
+ replaceEnvVariables,
17
+ replacePathVariables,
13
18
  replaceVariables
14
19
  };
15
20
  //# sourceMappingURL=replace-variables.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/regex/replace-variables.ts"],
4
- "sourcesContent": ["/**\n * This function takes a string and replace {variables} with given values.\n */\nexport function replaceVariables(\n value: string,\n variablesOrCallback: Record<string, string | number> | ((match: string) => string),\n) {\n // Replace all variables (example: {{ baseurl }} with an HTML tag)\n const doubleCurlyBrackets = /{{\\s*([\\w.-]+)\\s*}}/g\n const singleCurlyBrackets = /{\\s*([\\w.-]+)\\s*}/g\n\n const callback = (_: string, match: string): string => {\n if (typeof variablesOrCallback === 'function') {\n return variablesOrCallback(match)\n }\n return variablesOrCallback[match]?.toString() || `{${match}}`\n }\n\n // Loop through all matches and replace the match with the variable value\n return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback)\n}\n"],
5
- "mappings": "AAGO,SAAS,iBACd,OACA,qBACA;AAEA,QAAM,sBAAsB;AAC5B,QAAM,sBAAsB;AAE5B,QAAM,WAAW,CAAC,GAAW,UAA0B;AACrD,QAAI,OAAO,wBAAwB,YAAY;AAC7C,aAAO,oBAAoB,KAAK;AAAA,IAClC;AACA,WAAO,oBAAoB,KAAK,GAAG,SAAS,KAAK,IAAI,KAAK;AAAA,EAC5D;AAGA,SAAO,MAAM,QAAQ,qBAAqB,QAAQ,EAAE,QAAQ,qBAAqB,QAAQ;AAC3F;",
4
+ "sourcesContent": ["import { REGEX } from '@/regex/regex-helpers'\n\n/**\n * This function takes a string and replace {variables} with given values.\n */\nexport function replaceVariables(\n value: string,\n variablesOrCallback: Record<string, string | number> | ((match: string) => string),\n) {\n // Replace all variables (example: {{ baseurl }} with an HTML tag)\n const doubleCurlyBrackets = /{{\\s*([\\w.-]+)\\s*}}/g\n const singleCurlyBrackets = /{\\s*([\\w.-]+)\\s*}/g\n\n const callback = (_: string, match: string): string => {\n if (typeof variablesOrCallback === 'function') {\n return variablesOrCallback(match)\n }\n return variablesOrCallback[match]?.toString() || `{${match}}`\n }\n\n // Loop through all matches and replace the match with the variable value\n return value.replace(doubleCurlyBrackets, callback).replace(singleCurlyBrackets, callback)\n}\n\n/** Replace {path} variables with their values */\nexport const replacePathVariables = (path: string, variables: Record<string, string> = {}) =>\n path.replace(REGEX.PATH, (match, key) => variables[key] ?? match)\n\n/** Replace {{env}} variables with their values */\nexport const replaceEnvVariables = (path: string, variables: Record<string, string> = {}) =>\n path.replace(REGEX.VARIABLES, (match, key) => variables[key] ?? match)\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AAKf,SAAS,iBACd,OACA,qBACA;AAEA,QAAM,sBAAsB;AAC5B,QAAM,sBAAsB;AAE5B,QAAM,WAAW,CAAC,GAAW,UAA0B;AACrD,QAAI,OAAO,wBAAwB,YAAY;AAC7C,aAAO,oBAAoB,KAAK;AAAA,IAClC;AACA,WAAO,oBAAoB,KAAK,GAAG,SAAS,KAAK,IAAI,KAAK;AAAA,EAC5D;AAGA,SAAO,MAAM,QAAQ,qBAAqB,QAAQ,EAAE,QAAQ,qBAAqB,QAAQ;AAC3F;AAGO,MAAM,uBAAuB,CAAC,MAAc,YAAoC,CAAC,MACtF,KAAK,QAAQ,MAAM,MAAM,CAAC,OAAO,QAAQ,UAAU,GAAG,KAAK,KAAK;AAG3D,MAAM,sBAAsB,CAAC,MAAc,YAAoC,CAAC,MACrF,KAAK,QAAQ,MAAM,WAAW,CAAC,OAAO,QAAQ,UAAU,GAAG,KAAK,KAAK;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "helpers",
15
15
  "js"
16
16
  ],
17
- "version": "0.1.3",
17
+ "version": "0.2.1",
18
18
  "engines": {
19
19
  "node": ">=20"
20
20
  },