@sqlrooms/utils 0.24.3 → 0.24.5
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/memoization.d.ts +30 -0
- package/dist/memoization.d.ts.map +1 -0
- package/dist/memoization.js +53 -0
- package/dist/memoization.js.map +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport * from './color';\nexport * from './format';\nexport * from './helpers';\nexport {isMacOS} from './browser';\nexport * from './random';\nexport * from './str';\nexport * from './xhr';\nexport * from './filepaths';\nexport {safeJsonParse} from './json';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,OAAO,CAAC;AACtB,cAAc,OAAO,CAAC;AACtB,cAAc,aAAa,CAAC;AAC5B,OAAO,EAAC,aAAa,EAAC,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAC,WAAW,EAAC,MAAM,eAAe,CAAC","sourcesContent":["/**\n * {@include ../README.md}\n * @packageDocumentation\n */\n\nexport * from './color';\nexport * from './format';\nexport * from './helpers';\nexport {isMacOS} from './browser';\nexport * from './random';\nexport * from './str';\nexport * from './xhr';\nexport * from './filepaths';\nexport {safeJsonParse} from './json';\nexport {memoizeOnce} from './memoization';\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a memoized version of a function that caches only the last result.
|
|
3
|
+
* The cache is invalidated when any of the arguments change.
|
|
4
|
+
*
|
|
5
|
+
* This is useful for expensive operations that are likely to be called
|
|
6
|
+
* multiple times with the same arguments, like database queries or API calls.
|
|
7
|
+
*
|
|
8
|
+
* @param fn - The function to memoize
|
|
9
|
+
* @returns A memoized version of the function
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const expensiveQuery = async (userId: string, limit: number) => {
|
|
14
|
+
* return await database.query(`SELECT * FROM users WHERE id = ? LIMIT ?`, [userId, limit]);
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const memoizedQuery = memoizeOnce(expensiveQuery);
|
|
18
|
+
*
|
|
19
|
+
* // First call executes the function
|
|
20
|
+
* const result1 = await memoizedQuery("123", 10);
|
|
21
|
+
*
|
|
22
|
+
* // Second call with same arguments returns cached result
|
|
23
|
+
* const result2 = await memoizedQuery("123", 10); // Uses cache
|
|
24
|
+
*
|
|
25
|
+
* // Call with different arguments invalidates cache and executes function
|
|
26
|
+
* const result3 = await memoizedQuery("456", 10); // Executes function
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function memoizeOnce<TArgs extends readonly unknown[], TReturn>(fn: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn;
|
|
30
|
+
//# sourceMappingURL=memoization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoization.d.ts","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,WAAW,CAAC,KAAK,SAAS,SAAS,OAAO,EAAE,EAAE,OAAO,EACnE,EAAE,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,GAC9B,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAkB7B"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a memoized version of a function that caches only the last result.
|
|
3
|
+
* The cache is invalidated when any of the arguments change.
|
|
4
|
+
*
|
|
5
|
+
* This is useful for expensive operations that are likely to be called
|
|
6
|
+
* multiple times with the same arguments, like database queries or API calls.
|
|
7
|
+
*
|
|
8
|
+
* @param fn - The function to memoize
|
|
9
|
+
* @returns A memoized version of the function
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const expensiveQuery = async (userId: string, limit: number) => {
|
|
14
|
+
* return await database.query(`SELECT * FROM users WHERE id = ? LIMIT ?`, [userId, limit]);
|
|
15
|
+
* };
|
|
16
|
+
*
|
|
17
|
+
* const memoizedQuery = memoizeOnce(expensiveQuery);
|
|
18
|
+
*
|
|
19
|
+
* // First call executes the function
|
|
20
|
+
* const result1 = await memoizedQuery("123", 10);
|
|
21
|
+
*
|
|
22
|
+
* // Second call with same arguments returns cached result
|
|
23
|
+
* const result2 = await memoizedQuery("123", 10); // Uses cache
|
|
24
|
+
*
|
|
25
|
+
* // Call with different arguments invalidates cache and executes function
|
|
26
|
+
* const result3 = await memoizedQuery("456", 10); // Executes function
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function memoizeOnce(fn) {
|
|
30
|
+
let lastArgs;
|
|
31
|
+
let lastResult;
|
|
32
|
+
let hasResult = false;
|
|
33
|
+
return function (...args) {
|
|
34
|
+
// Check if we have a cached result and arguments haven't changed
|
|
35
|
+
if (hasResult && lastArgs && argsEqual(lastArgs, args)) {
|
|
36
|
+
return lastResult;
|
|
37
|
+
}
|
|
38
|
+
// Call the function with the correct context and cache the result
|
|
39
|
+
lastResult = fn.apply(this, args);
|
|
40
|
+
lastArgs = args;
|
|
41
|
+
hasResult = true;
|
|
42
|
+
return lastResult;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Shallow comparison of two arrays to check if all elements are equal
|
|
47
|
+
*/
|
|
48
|
+
function argsEqual(a, b) {
|
|
49
|
+
if (a.length !== b.length)
|
|
50
|
+
return false;
|
|
51
|
+
return a.every((val, index) => val === b[index]);
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=memoization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoization.js","sourceRoot":"","sources":["../src/memoization.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,UAAU,WAAW,CACzB,EAA+B;IAE/B,IAAI,QAA2B,CAAC;IAChC,IAAI,UAAmB,CAAC;IACxB,IAAI,SAAS,GAAG,KAAK,CAAC;IAEtB,OAAO,UAAqB,GAAG,IAAW;QACxC,iEAAiE;QACjE,IAAI,SAAS,IAAI,QAAQ,IAAI,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC;YACvD,OAAO,UAAU,CAAC;QACpB,CAAC;QAED,kEAAkE;QAClE,UAAU,GAAG,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,IAAwB,CAAC,CAAC;QACtD,QAAQ,GAAG,IAAI,CAAC;QAChB,SAAS,GAAG,IAAI,CAAC;QAEjB,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAS,SAAS,CAA+B,CAAI,EAAE,CAAI;IACzD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACnD,CAAC","sourcesContent":["/**\n * Creates a memoized version of a function that caches only the last result.\n * The cache is invalidated when any of the arguments change.\n *\n * This is useful for expensive operations that are likely to be called\n * multiple times with the same arguments, like database queries or API calls.\n *\n * @param fn - The function to memoize\n * @returns A memoized version of the function\n *\n * @example\n * ```ts\n * const expensiveQuery = async (userId: string, limit: number) => {\n * return await database.query(`SELECT * FROM users WHERE id = ? LIMIT ?`, [userId, limit]);\n * };\n *\n * const memoizedQuery = memoizeOnce(expensiveQuery);\n *\n * // First call executes the function\n * const result1 = await memoizedQuery(\"123\", 10);\n *\n * // Second call with same arguments returns cached result\n * const result2 = await memoizedQuery(\"123\", 10); // Uses cache\n *\n * // Call with different arguments invalidates cache and executes function\n * const result3 = await memoizedQuery(\"456\", 10); // Executes function\n * ```\n */\nexport function memoizeOnce<TArgs extends readonly unknown[], TReturn>(\n fn: (...args: TArgs) => TReturn,\n): (...args: TArgs) => TReturn {\n let lastArgs: TArgs | undefined;\n let lastResult: TReturn;\n let hasResult = false;\n\n return function (this: any, ...args: TArgs): TReturn {\n // Check if we have a cached result and arguments haven't changed\n if (hasResult && lastArgs && argsEqual(lastArgs, args)) {\n return lastResult;\n }\n\n // Call the function with the correct context and cache the result\n lastResult = fn.apply(this, args as unknown as any[]);\n lastArgs = args;\n hasResult = true;\n\n return lastResult;\n };\n}\n\n/**\n * Shallow comparison of two arrays to check if all elements are equal\n */\nfunction argsEqual<T extends readonly unknown[]>(a: T, b: T): boolean {\n if (a.length !== b.length) return false;\n return a.every((val, index) => val === b[index]);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/utils",
|
|
3
|
-
"version": "0.24.
|
|
3
|
+
"version": "0.24.5",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"react": ">=18",
|
|
45
45
|
"react-dom": ">=18"
|
|
46
46
|
},
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "393aee155a8954a9e5b7e792e6f98118182f2cc3"
|
|
48
48
|
}
|