@canvas-js/utils
Advanced tools
Comparing version 0.11.0 to 0.12.0
@@ -6,2 +6,3 @@ export * from "./assert.js"; | ||
export * from "./mapEntries.js"; | ||
export * from "./filterMapEntries.js"; | ||
export * from "./mapKeys.js"; | ||
@@ -8,0 +9,0 @@ export * from "./mapValues.js"; |
@@ -6,2 +6,3 @@ export * from "./assert.js"; | ||
export * from "./mapEntries.js"; | ||
export * from "./filterMapEntries.js"; | ||
export * from "./mapKeys.js"; | ||
@@ -8,0 +9,0 @@ export * from "./mapValues.js"; |
@@ -1,1 +0,1 @@ | ||
export declare const mapValuesAsync: <K extends string, S, T>(object: Record<K, S>, map: (value: S) => Promise<T>) => Promise<any>; | ||
export declare const mapValuesAsync: <K extends string, S, T>(object: Record<K, S>, map: (value: S) => Promise<T>) => Promise<Record<string, T>>; |
import { zip } from "./zip.js"; | ||
export const mapValuesAsync = async (object, map) => { | ||
const values = await Promise.all(Object.values(object).map((value) => map(value))); | ||
return Object.fromEntries(zip(Object.keys(object), values)); | ||
const result = {}; | ||
for (const [k, v] of zip(Object.keys(object), values)) { | ||
result[k] = v; | ||
} | ||
return result; | ||
}; |
@@ -0,1 +1,7 @@ | ||
export type JSONValue = null | boolean | number | string | JSONArray | JSONObject; | ||
export interface JSONArray extends Array<JSONValue> { | ||
} | ||
export interface JSONObject { | ||
[key: string]: JSONValue; | ||
} | ||
export type JSValue = null | boolean | number | string | Uint8Array | JSArray | JSObject; | ||
@@ -10,1 +16,4 @@ export interface JSArray extends Array<JSValue> { | ||
export declare function typeOf(value: JSValue): "null" | "boolean" | "number" | "string" | "Uint8Array" | "Array" | "Object"; | ||
export declare function isObject(value: JSValue): value is JSObject; | ||
export declare function merge(from: JSValue | undefined, into: JSValue | undefined): JSValue | undefined; | ||
export declare function update(from: JSValue | undefined, into: JSValue | undefined): JSValue | undefined; |
@@ -26,1 +26,99 @@ // Values | ||
} | ||
export function isObject(value) { | ||
return typeOf(value) === "Object"; | ||
} | ||
export function merge(from, into) { | ||
if (from === null) { | ||
return from; | ||
} | ||
else if (from === undefined) { | ||
return into; | ||
} | ||
else if (typeof from === "boolean") { | ||
return from; | ||
} | ||
else if (typeof from === "number") { | ||
return from; | ||
} | ||
else if (typeof from === "string") { | ||
return from; | ||
} | ||
else if (from instanceof Uint8Array) { | ||
return from; | ||
} | ||
else if (Array.isArray(from)) { | ||
return from; | ||
} | ||
else { | ||
// only merge objects | ||
if (into === undefined) | ||
return from; | ||
if (!isObject(from)) | ||
return from; | ||
if (!isObject(into)) | ||
return from; | ||
const mergedKeys = Array.from(new Set([...Object.keys(from), ...Object.keys(into)])); | ||
const result = {}; | ||
for (const key of mergedKeys) { | ||
if (from[key] === undefined) { | ||
result[key] = into[key]; | ||
} | ||
else if (into[key] === undefined) { | ||
result[key] = from[key]; | ||
} | ||
else { | ||
const merged = merge(from[key], into[key]); | ||
if (merged === undefined) { | ||
continue; | ||
} | ||
result[key] = merged; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
export function update(from, into) { | ||
if (from === null) { | ||
return from; | ||
} | ||
else if (from === undefined) { | ||
return into; | ||
} | ||
else if (typeof from === "boolean") { | ||
return from; | ||
} | ||
else if (typeof from === "number") { | ||
return from; | ||
} | ||
else if (typeof from === "string") { | ||
return from; | ||
} | ||
else if (from instanceof Uint8Array) { | ||
return from; | ||
} | ||
else if (Array.isArray(from)) { | ||
return from; | ||
} | ||
else { | ||
// update fields without recursive merging | ||
if (into === undefined) | ||
return from; | ||
if (!isObject(from)) | ||
return from; | ||
if (!isObject(into)) | ||
return from; | ||
const result = { ...into }; | ||
for (const key of Object.keys(from)) { | ||
if (from[key] === undefined) { | ||
result[key] = into[key]; | ||
} | ||
else if (into[key] === undefined) { | ||
result[key] = from[key]; | ||
} | ||
else { | ||
result[key] = from[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
} |
{ | ||
"name": "@canvas-js/utils", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"author": "Canvas Technologies, Inc. (https://canvas.xyz)", | ||
@@ -19,4 +19,4 @@ "type": "module", | ||
"dependencies": { | ||
"@canvas-js/interfaces": "0.11.0" | ||
"@canvas-js/interfaces": "0.12.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14724
30
351
+ Added@canvas-js/interfaces@0.12.0(transitive)
- Removed@canvas-js/interfaces@0.11.0(transitive)
Updated@canvas-js/interfaces@0.12.0