@junobuild/utils 0.0.27 → 0.1.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,2 +1,2 @@
1
- var m=(r,n)=>{let o;return(...e)=>{let a=()=>r(...e);o&&clearTimeout(o),o=setTimeout(a,n!==void 0&&n>0?n:300)}};import{Principal as u}from"@dfinity/principal";var c=r=>r==null,t=r=>!c(r),i=class extends Error{},x=(r,n)=>{if(c(r))throw new i(n)};var s="__bigint__",f="__principal__",p="__uint8array__",T=(r,n)=>typeof n=="bigint"?{[s]:`${n}`}:t(n)&&n instanceof u?{[f]:n.toText()}:t(n)&&n instanceof Uint8Array?{[p]:Array.from(n)}:n,l=(r,n)=>{let o=e=>n[e];return t(n)&&typeof n=="object"&&s in n?BigInt(o(s)):t(n)&&typeof n=="object"&&f in n?u.fromText(o(f)):t(n)&&typeof n=="object"&&p in n?Uint8Array.from(o(p)):n};var g=r=>t(r)?[r]:[],j=r=>r?.[0],R=async r=>{let n=new Blob([JSON.stringify(r,T)],{type:"application/json; charset=utf-8"});return new Uint8Array(await n.arrayBuffer())},B=async r=>{let n=new Blob([r instanceof Uint8Array?r:new Uint8Array(r)],{type:"application/json; charset=utf-8"});return JSON.parse(await n.text(),l)};var h=()=>typeof window<"u";export{i as NullishError,x as assertNonNullish,m as debounce,B as fromArray,j as fromNullable,h as isBrowser,c as isNullish,T as jsonReplacer,l as jsonReviver,t as nonNullish,R as toArray,g as toNullable};
1
+ import{jsonReplacer as n,jsonReviver as t}from"@dfinity/utils";var a=async r=>{let o=new Blob([JSON.stringify(r,n)],{type:"application/json; charset=utf-8"});return new Uint8Array(await o.arrayBuffer())},i=async r=>{let o=new Blob([r instanceof Uint8Array?r:new Uint8Array(r)],{type:"application/json; charset=utf-8"});return JSON.parse(await o.text(),t)};var p=()=>typeof window<"u";export{i as fromArray,p as isBrowser,a as toArray};
2
2
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/utils/debounce.utils.ts", "../../src/utils/json.utils.ts", "../../src/utils/null.utils.ts", "../../src/utils/did.utils.ts", "../../src/utils/env.utils.ts"],
4
- "sourcesContent": ["/**\n * Creates a debounced function that delays invoking the provided function until after the specified timeout.\n * @param {Function} func - The function to debounce.\n * @param {number} [timeout=300] - The number of milliseconds to delay. Defaults to 300ms if not specified or invalid.\n * @returns {Function} A debounced function.\n */\n// eslint-disable-next-line @typescript-eslint/ban-types, local-rules/prefer-object-params\nexport const debounce = (func: Function, timeout?: number): Function => {\n let timer: ReturnType<typeof setTimeout> | undefined;\n\n return (...args: unknown[]) => {\n const next = () => func(...args);\n\n if (timer) {\n clearTimeout(timer);\n }\n\n timer = setTimeout(next, timeout !== undefined && timeout > 0 ? timeout : 300);\n };\n};\n", "import {Principal} from '@dfinity/principal';\nimport {nonNullish} from './null.utils';\n\nconst JSON_KEY_BIGINT = '__bigint__';\nconst JSON_KEY_PRINCIPAL = '__principal__';\nconst JSON_KEY_UINT8ARRAY = '__uint8array__';\n\n/**\n * A function that alters the behavior of the stringification process for BigInt, Principal, and Uint8Array.\n * @param {string} _key - The key of the value being stringified.\n * @param {unknown} value - The value being stringified.\n * @returns {unknown} The altered value for stringification.\n */\n// eslint-disable-next-line local-rules/prefer-object-params\nexport const jsonReplacer = (_key: string, value: unknown): unknown => {\n if (typeof value === 'bigint') {\n return {[JSON_KEY_BIGINT]: `${value}`};\n }\n\n if (nonNullish(value) && value instanceof Principal) {\n return {[JSON_KEY_PRINCIPAL]: value.toText()};\n }\n\n if (nonNullish(value) && value instanceof Uint8Array) {\n return {[JSON_KEY_UINT8ARRAY]: Array.from(value)};\n }\n\n return value;\n};\n\n/**\n * A parser that interprets revived BigInt, Principal, and Uint8Array when constructing JavaScript values or objects.\n * @param {string} _key - The key of the value being parsed.\n * @param {unknown} value - The value being parsed.\n * @returns {unknown} The parsed value.\n */\n// eslint-disable-next-line local-rules/prefer-object-params\nexport const jsonReviver = (_key: string, value: unknown): unknown => {\n const mapValue = <T>(key: string): T => (value as Record<string, T>)[key];\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_BIGINT in value) {\n return BigInt(mapValue(JSON_KEY_BIGINT));\n }\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_PRINCIPAL in value) {\n return Principal.fromText(mapValue(JSON_KEY_PRINCIPAL));\n }\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_UINT8ARRAY in value) {\n return Uint8Array.from(mapValue(JSON_KEY_UINT8ARRAY));\n }\n\n return value;\n};\n", "/**\n * Checks if the provided argument is null or undefined.\n * @template T\n * @param {T | undefined | null} argument - The argument to check.\n * @returns {boolean} True if the argument is null or undefined, false otherwise.\n */\n// eslint-disable-next-line local-rules/use-option-type-wrapper\nexport const isNullish = <T>(argument: T | undefined | null): argument is undefined | null =>\n argument === null || argument === undefined;\n\n/**\n * Checks if the provided argument is neither null nor undefined.\n * @template T\n * @param {T | undefined | null} argument - The argument to check.\n * @returns {boolean} True if the argument is neither null nor undefined, false otherwise.\n */\n// eslint-disable-next-line local-rules/use-option-type-wrapper\nexport const nonNullish = <T>(argument: T | undefined | null): argument is NonNullable<T> =>\n !isNullish(argument);\n\n/**\n * Represents an error thrown when a value is null or undefined.\n * @class\n * @extends {Error}\n */\nexport class NullishError extends Error {}\n\n/**\n * Asserts that a value is neither null nor undefined.\n * @template T\n * @param {T} value - The value to check.\n * @param {string} [message] - The optional error message to use if the assertion fails.\n * @throws {NullishError} If the value is null or undefined.\n * @returns {asserts value is NonNullable<T>} Asserts that the value is neither null nor undefined.\n */\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n // eslint-disable-next-line local-rules/prefer-object-params\n) => asserts value is NonNullable<T> = <T>(value: T, message?: string): void => {\n if (isNullish(value)) {\n throw new NullishError(message);\n }\n};\n", "import {jsonReplacer, jsonReviver} from './json.utils';\nimport {nonNullish} from './null.utils';\n\n/**\n * Converts a value to a nullable array.\n * @template T\n * @param {T} [value] - The value to convert.\n * @returns {([] | [T])} A nullable array containing the value if non-nullish, or an empty array if nullish.\n */\nexport const toNullable = <T>(value?: T): [] | [T] => (nonNullish(value) ? [value] : []);\n\n/**\n * Extracts a value from a nullable array.\n * @template T\n * @param {([] | [T])} value - The nullable array.\n * @returns {(T | undefined)} The value if present, or undefined if the array is empty.\n */\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => value?.[0];\n\n/**\n * Converts data to a Uint8Array for transmission or storage.\n * @template T\n * @param {T} data - The data to convert.\n * @returns {Promise<Uint8Array>} A promise that resolves to a Uint8Array representation of the data.\n */\nexport const toArray = async <T>(data: T): Promise<Uint8Array> => {\n const blob: Blob = new Blob([JSON.stringify(data, jsonReplacer)], {\n type: 'application/json; charset=utf-8'\n });\n return new Uint8Array(await blob.arrayBuffer());\n};\n\n/**\n * Converts a Uint8Array or number array back to the original data type.\n * @template T\n * @param {(Uint8Array | number[])} data - The array to convert.\n * @returns {Promise<T>} A promise that resolves to the original data.\n */\nexport const fromArray = async <T>(data: Uint8Array | number[]): Promise<T> => {\n const blob: Blob = new Blob([data instanceof Uint8Array ? data : new Uint8Array(data)], {\n type: 'application/json; charset=utf-8'\n });\n return JSON.parse(await blob.text(), jsonReviver);\n};\n", "/**\n * Checks if the current environment is a browser.\n * @returns {boolean} True if the current environment is a browser, false otherwise.\n */\nexport const isBrowser = (): boolean => typeof window !== `undefined`;\n"],
5
- "mappings": "AAOO,IAAMA,EAAW,CAACC,EAAgBC,IAA+B,CACtE,IAAIC,EAEJ,MAAO,IAAIC,IAAoB,CAC7B,IAAMC,EAAO,IAAMJ,EAAK,GAAGG,CAAI,EAE3BD,GACF,aAAaA,CAAK,EAGpBA,EAAQ,WAAWE,EAAMH,IAAY,QAAaA,EAAU,EAAIA,EAAU,GAAG,CAC/E,CACF,ECnBA,OAAQ,aAAAI,MAAgB,qBCOjB,IAAMC,EAAgBC,GAC3BA,GAAa,KASFC,EAAiBD,GAC5B,CAACD,EAAUC,CAAQ,EAORE,EAAN,cAA2B,KAAM,CAAC,EAU5BC,EAI0B,CAAIC,EAAUC,IAA2B,CAC9E,GAAIN,EAAUK,CAAK,EACjB,MAAM,IAAIF,EAAaG,CAAO,CAElC,EDxCA,IAAMC,EAAkB,aAClBC,EAAqB,gBACrBC,EAAsB,iBASfC,EAAe,CAACC,EAAcC,IACrC,OAAOA,GAAU,SACZ,CAAC,CAACL,CAAe,EAAG,GAAGK,CAAK,EAAE,EAGnCC,EAAWD,CAAK,GAAKA,aAAiBE,EACjC,CAAC,CAACN,CAAkB,EAAGI,EAAM,OAAO,CAAC,EAG1CC,EAAWD,CAAK,GAAKA,aAAiB,WACjC,CAAC,CAACH,CAAmB,EAAG,MAAM,KAAKG,CAAK,CAAC,EAG3CA,EAUIG,EAAc,CAACJ,EAAcC,IAA4B,CACpE,IAAMI,EAAeC,GAAoBL,EAA4BK,CAAG,EAExE,OAAIJ,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYL,KAAmBK,EAChE,OAAOI,EAAST,CAAe,CAAC,EAGrCM,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYJ,KAAsBI,EACnEE,EAAU,SAASE,EAASR,CAAkB,CAAC,EAGpDK,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYH,KAAuBG,EACpE,WAAW,KAAKI,EAASP,CAAmB,CAAC,EAG/CG,CACT,EE5CO,IAAMM,EAAiBC,GAAyBC,EAAWD,CAAK,EAAI,CAACA,CAAK,EAAI,CAAC,EAQzEE,EAAmBF,GAAmCA,IAAQ,CAAC,EAQ/DG,EAAU,MAAUC,GAAiC,CAChE,IAAMC,EAAa,IAAI,KAAK,CAAC,KAAK,UAAUD,EAAME,CAAY,CAAC,EAAG,CAChE,KAAM,iCACR,CAAC,EACD,OAAO,IAAI,WAAW,MAAMD,EAAK,YAAY,CAAC,CAChD,EAQaE,EAAY,MAAUH,GAA4C,CAC7E,IAAMC,EAAa,IAAI,KAAK,CAACD,aAAgB,WAAaA,EAAO,IAAI,WAAWA,CAAI,CAAC,EAAG,CACtF,KAAM,iCACR,CAAC,EACD,OAAO,KAAK,MAAM,MAAMC,EAAK,KAAK,EAAGG,CAAW,CAClD,ECvCO,IAAMC,EAAY,IAAe,OAAO,OAAW",
6
- "names": ["debounce", "func", "timeout", "timer", "args", "next", "Principal", "isNullish", "argument", "nonNullish", "NullishError", "assertNonNullish", "value", "message", "JSON_KEY_BIGINT", "JSON_KEY_PRINCIPAL", "JSON_KEY_UINT8ARRAY", "jsonReplacer", "_key", "value", "nonNullish", "Principal", "jsonReviver", "mapValue", "key", "toNullable", "value", "nonNullish", "fromNullable", "toArray", "data", "blob", "jsonReplacer", "fromArray", "jsonReviver", "isBrowser"]
3
+ "sources": ["../../src/utils/did.utils.ts", "../../src/utils/env.utils.ts"],
4
+ "sourcesContent": ["import {jsonReplacer, jsonReviver} from '@dfinity/utils';\n\n/**\n * Converts data to a Uint8Array for transmission or storage.\n * @template T\n * @param {T} data - The data to convert.\n * @returns {Promise<Uint8Array>} A promise that resolves to a Uint8Array representation of the data.\n */\nexport const toArray = async <T>(data: T): Promise<Uint8Array> => {\n const blob: Blob = new Blob([JSON.stringify(data, jsonReplacer)], {\n type: 'application/json; charset=utf-8'\n });\n return new Uint8Array(await blob.arrayBuffer());\n};\n\n/**\n * Converts a Uint8Array or number array back to the original data type.\n * @template T\n * @param {(Uint8Array | number[])} data - The array to convert.\n * @returns {Promise<T>} A promise that resolves to the original data.\n */\nexport const fromArray = async <T>(data: Uint8Array | number[]): Promise<T> => {\n const blob: Blob = new Blob([data instanceof Uint8Array ? data : new Uint8Array(data)], {\n type: 'application/json; charset=utf-8'\n });\n return JSON.parse(await blob.text(), jsonReviver);\n};\n", "/**\n * Checks if the current environment is a browser.\n * @returns {boolean} True if the current environment is a browser, false otherwise.\n */\nexport const isBrowser = (): boolean => typeof window !== `undefined`;\n"],
5
+ "mappings": "AAAA,OAAQ,gBAAAA,EAAc,eAAAC,MAAkB,iBAQjC,IAAMC,EAAU,MAAUC,GAAiC,CAChE,IAAMC,EAAa,IAAI,KAAK,CAAC,KAAK,UAAUD,EAAMH,CAAY,CAAC,EAAG,CAChE,KAAM,iCACR,CAAC,EACD,OAAO,IAAI,WAAW,MAAMI,EAAK,YAAY,CAAC,CAChD,EAQaC,EAAY,MAAUF,GAA4C,CAC7E,IAAMC,EAAa,IAAI,KAAK,CAACD,aAAgB,WAAaA,EAAO,IAAI,WAAWA,CAAI,CAAC,EAAG,CACtF,KAAM,iCACR,CAAC,EACD,OAAO,KAAK,MAAM,MAAMC,EAAK,KAAK,EAAGH,CAAW,CAClD,ECtBO,IAAMK,EAAY,IAAe,OAAO,OAAW",
6
+ "names": ["jsonReplacer", "jsonReviver", "toArray", "data", "blob", "fromArray", "isBrowser"]
7
7
  }
@@ -1,4 +1,4 @@
1
1
  import { createRequire as topLevelCreateRequire } from 'module';
2
2
  const require = topLevelCreateRequire(import.meta.url);
3
- var m=(r,n)=>{let o;return(...e)=>{let a=()=>r(...e);o&&clearTimeout(o),o=setTimeout(a,n!==void 0&&n>0?n:300)}};import{Principal as u}from"@dfinity/principal";var c=r=>r==null,t=r=>!c(r),i=class extends Error{},x=(r,n)=>{if(c(r))throw new i(n)};var s="__bigint__",f="__principal__",p="__uint8array__",T=(r,n)=>typeof n=="bigint"?{[s]:`${n}`}:t(n)&&n instanceof u?{[f]:n.toText()}:t(n)&&n instanceof Uint8Array?{[p]:Array.from(n)}:n,l=(r,n)=>{let o=e=>n[e];return t(n)&&typeof n=="object"&&s in n?BigInt(o(s)):t(n)&&typeof n=="object"&&f in n?u.fromText(o(f)):t(n)&&typeof n=="object"&&p in n?Uint8Array.from(o(p)):n};var g=r=>t(r)?[r]:[],j=r=>r?.[0],R=async r=>{let n=new Blob([JSON.stringify(r,T)],{type:"application/json; charset=utf-8"});return new Uint8Array(await n.arrayBuffer())},B=async r=>{let n=new Blob([r instanceof Uint8Array?r:new Uint8Array(r)],{type:"application/json; charset=utf-8"});return JSON.parse(await n.text(),l)};var h=()=>typeof window<"u";export{i as NullishError,x as assertNonNullish,m as debounce,B as fromArray,j as fromNullable,h as isBrowser,c as isNullish,T as jsonReplacer,l as jsonReviver,t as nonNullish,R as toArray,g as toNullable};
3
+ import{jsonReplacer as n,jsonReviver as t}from"@dfinity/utils";var a=async r=>{let o=new Blob([JSON.stringify(r,n)],{type:"application/json; charset=utf-8"});return new Uint8Array(await o.arrayBuffer())},i=async r=>{let o=new Blob([r instanceof Uint8Array?r:new Uint8Array(r)],{type:"application/json; charset=utf-8"});return JSON.parse(await o.text(),t)};var p=()=>typeof window<"u";export{i as fromArray,p as isBrowser,a as toArray};
4
4
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../src/utils/debounce.utils.ts", "../../src/utils/json.utils.ts", "../../src/utils/null.utils.ts", "../../src/utils/did.utils.ts", "../../src/utils/env.utils.ts"],
4
- "sourcesContent": ["/**\n * Creates a debounced function that delays invoking the provided function until after the specified timeout.\n * @param {Function} func - The function to debounce.\n * @param {number} [timeout=300] - The number of milliseconds to delay. Defaults to 300ms if not specified or invalid.\n * @returns {Function} A debounced function.\n */\n// eslint-disable-next-line @typescript-eslint/ban-types, local-rules/prefer-object-params\nexport const debounce = (func: Function, timeout?: number): Function => {\n let timer: ReturnType<typeof setTimeout> | undefined;\n\n return (...args: unknown[]) => {\n const next = () => func(...args);\n\n if (timer) {\n clearTimeout(timer);\n }\n\n timer = setTimeout(next, timeout !== undefined && timeout > 0 ? timeout : 300);\n };\n};\n", "import {Principal} from '@dfinity/principal';\nimport {nonNullish} from './null.utils';\n\nconst JSON_KEY_BIGINT = '__bigint__';\nconst JSON_KEY_PRINCIPAL = '__principal__';\nconst JSON_KEY_UINT8ARRAY = '__uint8array__';\n\n/**\n * A function that alters the behavior of the stringification process for BigInt, Principal, and Uint8Array.\n * @param {string} _key - The key of the value being stringified.\n * @param {unknown} value - The value being stringified.\n * @returns {unknown} The altered value for stringification.\n */\n// eslint-disable-next-line local-rules/prefer-object-params\nexport const jsonReplacer = (_key: string, value: unknown): unknown => {\n if (typeof value === 'bigint') {\n return {[JSON_KEY_BIGINT]: `${value}`};\n }\n\n if (nonNullish(value) && value instanceof Principal) {\n return {[JSON_KEY_PRINCIPAL]: value.toText()};\n }\n\n if (nonNullish(value) && value instanceof Uint8Array) {\n return {[JSON_KEY_UINT8ARRAY]: Array.from(value)};\n }\n\n return value;\n};\n\n/**\n * A parser that interprets revived BigInt, Principal, and Uint8Array when constructing JavaScript values or objects.\n * @param {string} _key - The key of the value being parsed.\n * @param {unknown} value - The value being parsed.\n * @returns {unknown} The parsed value.\n */\n// eslint-disable-next-line local-rules/prefer-object-params\nexport const jsonReviver = (_key: string, value: unknown): unknown => {\n const mapValue = <T>(key: string): T => (value as Record<string, T>)[key];\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_BIGINT in value) {\n return BigInt(mapValue(JSON_KEY_BIGINT));\n }\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_PRINCIPAL in value) {\n return Principal.fromText(mapValue(JSON_KEY_PRINCIPAL));\n }\n\n if (nonNullish(value) && typeof value === 'object' && JSON_KEY_UINT8ARRAY in value) {\n return Uint8Array.from(mapValue(JSON_KEY_UINT8ARRAY));\n }\n\n return value;\n};\n", "/**\n * Checks if the provided argument is null or undefined.\n * @template T\n * @param {T | undefined | null} argument - The argument to check.\n * @returns {boolean} True if the argument is null or undefined, false otherwise.\n */\n// eslint-disable-next-line local-rules/use-option-type-wrapper\nexport const isNullish = <T>(argument: T | undefined | null): argument is undefined | null =>\n argument === null || argument === undefined;\n\n/**\n * Checks if the provided argument is neither null nor undefined.\n * @template T\n * @param {T | undefined | null} argument - The argument to check.\n * @returns {boolean} True if the argument is neither null nor undefined, false otherwise.\n */\n// eslint-disable-next-line local-rules/use-option-type-wrapper\nexport const nonNullish = <T>(argument: T | undefined | null): argument is NonNullable<T> =>\n !isNullish(argument);\n\n/**\n * Represents an error thrown when a value is null or undefined.\n * @class\n * @extends {Error}\n */\nexport class NullishError extends Error {}\n\n/**\n * Asserts that a value is neither null nor undefined.\n * @template T\n * @param {T} value - The value to check.\n * @param {string} [message] - The optional error message to use if the assertion fails.\n * @throws {NullishError} If the value is null or undefined.\n * @returns {asserts value is NonNullable<T>} Asserts that the value is neither null nor undefined.\n */\nexport const assertNonNullish: <T>(\n value: T,\n message?: string\n // eslint-disable-next-line local-rules/prefer-object-params\n) => asserts value is NonNullable<T> = <T>(value: T, message?: string): void => {\n if (isNullish(value)) {\n throw new NullishError(message);\n }\n};\n", "import {jsonReplacer, jsonReviver} from './json.utils';\nimport {nonNullish} from './null.utils';\n\n/**\n * Converts a value to a nullable array.\n * @template T\n * @param {T} [value] - The value to convert.\n * @returns {([] | [T])} A nullable array containing the value if non-nullish, or an empty array if nullish.\n */\nexport const toNullable = <T>(value?: T): [] | [T] => (nonNullish(value) ? [value] : []);\n\n/**\n * Extracts a value from a nullable array.\n * @template T\n * @param {([] | [T])} value - The nullable array.\n * @returns {(T | undefined)} The value if present, or undefined if the array is empty.\n */\nexport const fromNullable = <T>(value: [] | [T]): T | undefined => value?.[0];\n\n/**\n * Converts data to a Uint8Array for transmission or storage.\n * @template T\n * @param {T} data - The data to convert.\n * @returns {Promise<Uint8Array>} A promise that resolves to a Uint8Array representation of the data.\n */\nexport const toArray = async <T>(data: T): Promise<Uint8Array> => {\n const blob: Blob = new Blob([JSON.stringify(data, jsonReplacer)], {\n type: 'application/json; charset=utf-8'\n });\n return new Uint8Array(await blob.arrayBuffer());\n};\n\n/**\n * Converts a Uint8Array or number array back to the original data type.\n * @template T\n * @param {(Uint8Array | number[])} data - The array to convert.\n * @returns {Promise<T>} A promise that resolves to the original data.\n */\nexport const fromArray = async <T>(data: Uint8Array | number[]): Promise<T> => {\n const blob: Blob = new Blob([data instanceof Uint8Array ? data : new Uint8Array(data)], {\n type: 'application/json; charset=utf-8'\n });\n return JSON.parse(await blob.text(), jsonReviver);\n};\n", "/**\n * Checks if the current environment is a browser.\n * @returns {boolean} True if the current environment is a browser, false otherwise.\n */\nexport const isBrowser = (): boolean => typeof window !== `undefined`;\n"],
5
- "mappings": ";;AAOO,IAAMA,EAAW,CAACC,EAAgBC,IAA+B,CACtE,IAAIC,EAEJ,MAAO,IAAIC,IAAoB,CAC7B,IAAMC,EAAO,IAAMJ,EAAK,GAAGG,CAAI,EAE3BD,GACF,aAAaA,CAAK,EAGpBA,EAAQ,WAAWE,EAAMH,IAAY,QAAaA,EAAU,EAAIA,EAAU,GAAG,CAC/E,CACF,ECnBA,OAAQ,aAAAI,MAAgB,qBCOjB,IAAMC,EAAgBC,GAC3BA,GAAa,KASFC,EAAiBD,GAC5B,CAACD,EAAUC,CAAQ,EAORE,EAAN,cAA2B,KAAM,CAAC,EAU5BC,EAI0B,CAAIC,EAAUC,IAA2B,CAC9E,GAAIN,EAAUK,CAAK,EACjB,MAAM,IAAIF,EAAaG,CAAO,CAElC,EDxCA,IAAMC,EAAkB,aAClBC,EAAqB,gBACrBC,EAAsB,iBASfC,EAAe,CAACC,EAAcC,IACrC,OAAOA,GAAU,SACZ,CAAC,CAACL,CAAe,EAAG,GAAGK,CAAK,EAAE,EAGnCC,EAAWD,CAAK,GAAKA,aAAiBE,EACjC,CAAC,CAACN,CAAkB,EAAGI,EAAM,OAAO,CAAC,EAG1CC,EAAWD,CAAK,GAAKA,aAAiB,WACjC,CAAC,CAACH,CAAmB,EAAG,MAAM,KAAKG,CAAK,CAAC,EAG3CA,EAUIG,EAAc,CAACJ,EAAcC,IAA4B,CACpE,IAAMI,EAAeC,GAAoBL,EAA4BK,CAAG,EAExE,OAAIJ,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYL,KAAmBK,EAChE,OAAOI,EAAST,CAAe,CAAC,EAGrCM,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYJ,KAAsBI,EACnEE,EAAU,SAASE,EAASR,CAAkB,CAAC,EAGpDK,EAAWD,CAAK,GAAK,OAAOA,GAAU,UAAYH,KAAuBG,EACpE,WAAW,KAAKI,EAASP,CAAmB,CAAC,EAG/CG,CACT,EE5CO,IAAMM,EAAiBC,GAAyBC,EAAWD,CAAK,EAAI,CAACA,CAAK,EAAI,CAAC,EAQzEE,EAAmBF,GAAmCA,IAAQ,CAAC,EAQ/DG,EAAU,MAAUC,GAAiC,CAChE,IAAMC,EAAa,IAAI,KAAK,CAAC,KAAK,UAAUD,EAAME,CAAY,CAAC,EAAG,CAChE,KAAM,iCACR,CAAC,EACD,OAAO,IAAI,WAAW,MAAMD,EAAK,YAAY,CAAC,CAChD,EAQaE,EAAY,MAAUH,GAA4C,CAC7E,IAAMC,EAAa,IAAI,KAAK,CAACD,aAAgB,WAAaA,EAAO,IAAI,WAAWA,CAAI,CAAC,EAAG,CACtF,KAAM,iCACR,CAAC,EACD,OAAO,KAAK,MAAM,MAAMC,EAAK,KAAK,EAAGG,CAAW,CAClD,ECvCO,IAAMC,EAAY,IAAe,OAAO,OAAW",
6
- "names": ["debounce", "func", "timeout", "timer", "args", "next", "Principal", "isNullish", "argument", "nonNullish", "NullishError", "assertNonNullish", "value", "message", "JSON_KEY_BIGINT", "JSON_KEY_PRINCIPAL", "JSON_KEY_UINT8ARRAY", "jsonReplacer", "_key", "value", "nonNullish", "Principal", "jsonReviver", "mapValue", "key", "toNullable", "value", "nonNullish", "fromNullable", "toArray", "data", "blob", "jsonReplacer", "fromArray", "jsonReviver", "isBrowser"]
3
+ "sources": ["../../src/utils/did.utils.ts", "../../src/utils/env.utils.ts"],
4
+ "sourcesContent": ["import {jsonReplacer, jsonReviver} from '@dfinity/utils';\n\n/**\n * Converts data to a Uint8Array for transmission or storage.\n * @template T\n * @param {T} data - The data to convert.\n * @returns {Promise<Uint8Array>} A promise that resolves to a Uint8Array representation of the data.\n */\nexport const toArray = async <T>(data: T): Promise<Uint8Array> => {\n const blob: Blob = new Blob([JSON.stringify(data, jsonReplacer)], {\n type: 'application/json; charset=utf-8'\n });\n return new Uint8Array(await blob.arrayBuffer());\n};\n\n/**\n * Converts a Uint8Array or number array back to the original data type.\n * @template T\n * @param {(Uint8Array | number[])} data - The array to convert.\n * @returns {Promise<T>} A promise that resolves to the original data.\n */\nexport const fromArray = async <T>(data: Uint8Array | number[]): Promise<T> => {\n const blob: Blob = new Blob([data instanceof Uint8Array ? data : new Uint8Array(data)], {\n type: 'application/json; charset=utf-8'\n });\n return JSON.parse(await blob.text(), jsonReviver);\n};\n", "/**\n * Checks if the current environment is a browser.\n * @returns {boolean} True if the current environment is a browser, false otherwise.\n */\nexport const isBrowser = (): boolean => typeof window !== `undefined`;\n"],
5
+ "mappings": ";;AAAA,OAAQ,gBAAAA,EAAc,eAAAC,MAAkB,iBAQjC,IAAMC,EAAU,MAAUC,GAAiC,CAChE,IAAMC,EAAa,IAAI,KAAK,CAAC,KAAK,UAAUD,EAAMH,CAAY,CAAC,EAAG,CAChE,KAAM,iCACR,CAAC,EACD,OAAO,IAAI,WAAW,MAAMI,EAAK,YAAY,CAAC,CAChD,EAQaC,EAAY,MAAUF,GAA4C,CAC7E,IAAMC,EAAa,IAAI,KAAK,CAACD,aAAgB,WAAaA,EAAO,IAAI,WAAWA,CAAI,CAAC,EAAG,CACtF,KAAM,iCACR,CAAC,EACD,OAAO,KAAK,MAAM,MAAMC,EAAK,KAAK,EAAGH,CAAW,CAClD,ECtBO,IAAMK,EAAY,IAAe,OAAO,OAAW",
6
+ "names": ["jsonReplacer", "jsonReviver", "toArray", "data", "blob", "fromArray", "isBrowser"]
7
7
  }
@@ -1,5 +1,2 @@
1
- export * from './utils/debounce.utils';
2
1
  export * from './utils/did.utils';
3
2
  export * from './utils/env.utils';
4
- export * from './utils/json.utils';
5
- export * from './utils/null.utils';
@@ -1,17 +1,3 @@
1
- /**
2
- * Converts a value to a nullable array.
3
- * @template T
4
- * @param {T} [value] - The value to convert.
5
- * @returns {([] | [T])} A nullable array containing the value if non-nullish, or an empty array if nullish.
6
- */
7
- export declare const toNullable: <T>(value?: T) => [] | [T];
8
- /**
9
- * Extracts a value from a nullable array.
10
- * @template T
11
- * @param {([] | [T])} value - The nullable array.
12
- * @returns {(T | undefined)} The value if present, or undefined if the array is empty.
13
- */
14
- export declare const fromNullable: <T>(value: [] | [T]) => T | undefined;
15
1
  /**
16
2
  * Converts data to a Uint8Array for transmission or storage.
17
3
  * @template T
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@junobuild/utils",
3
- "version": "0.0.27",
3
+ "version": "0.1.0",
4
4
  "description": "A collection of utilities and constants for Juno JS libs.",
5
5
  "author": "David Dal Busco (https://daviddalbusco.com)",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "repository": {
35
35
  "type": "git",
36
36
  "url": "git+https://github.com/junobuild/juno-js.git",
37
- "directory": "packages/core"
37
+ "directory": "packages/utils"
38
38
  },
39
39
  "bugs": {
40
40
  "url": "https://github.com/junobuild/juno-js"
@@ -52,6 +52,6 @@
52
52
  ],
53
53
  "homepage": "https://juno.build",
54
54
  "peerDependencies": {
55
- "@dfinity/principal": "^2.1.3"
55
+ "@dfinity/utils": "^2"
56
56
  }
57
57
  }
@@ -1,7 +0,0 @@
1
- /**
2
- * Creates a debounced function that delays invoking the provided function until after the specified timeout.
3
- * @param {Function} func - The function to debounce.
4
- * @param {number} [timeout=300] - The number of milliseconds to delay. Defaults to 300ms if not specified or invalid.
5
- * @returns {Function} A debounced function.
6
- */
7
- export declare const debounce: (func: Function, timeout?: number) => Function;
@@ -1,14 +0,0 @@
1
- /**
2
- * A function that alters the behavior of the stringification process for BigInt, Principal, and Uint8Array.
3
- * @param {string} _key - The key of the value being stringified.
4
- * @param {unknown} value - The value being stringified.
5
- * @returns {unknown} The altered value for stringification.
6
- */
7
- export declare const jsonReplacer: (_key: string, value: unknown) => unknown;
8
- /**
9
- * A parser that interprets revived BigInt, Principal, and Uint8Array when constructing JavaScript values or objects.
10
- * @param {string} _key - The key of the value being parsed.
11
- * @param {unknown} value - The value being parsed.
12
- * @returns {unknown} The parsed value.
13
- */
14
- export declare const jsonReviver: (_key: string, value: unknown) => unknown;
@@ -1,30 +0,0 @@
1
- /**
2
- * Checks if the provided argument is null or undefined.
3
- * @template T
4
- * @param {T | undefined | null} argument - The argument to check.
5
- * @returns {boolean} True if the argument is null or undefined, false otherwise.
6
- */
7
- export declare const isNullish: <T>(argument: T | undefined | null) => argument is undefined | null;
8
- /**
9
- * Checks if the provided argument is neither null nor undefined.
10
- * @template T
11
- * @param {T | undefined | null} argument - The argument to check.
12
- * @returns {boolean} True if the argument is neither null nor undefined, false otherwise.
13
- */
14
- export declare const nonNullish: <T>(argument: T | undefined | null) => argument is NonNullable<T>;
15
- /**
16
- * Represents an error thrown when a value is null or undefined.
17
- * @class
18
- * @extends {Error}
19
- */
20
- export declare class NullishError extends Error {
21
- }
22
- /**
23
- * Asserts that a value is neither null nor undefined.
24
- * @template T
25
- * @param {T} value - The value to check.
26
- * @param {string} [message] - The optional error message to use if the assertion fails.
27
- * @throws {NullishError} If the value is null or undefined.
28
- * @returns {asserts value is NonNullable<T>} Asserts that the value is neither null nor undefined.
29
- */
30
- export declare const assertNonNullish: <T>(value: T, message?: string) => asserts value is NonNullable<T>;