@algorandfoundation/algokit-utils 8.1.1-beta.1 → 8.2.0-beta.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/util.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var types_app = require('./types/app.js');
4
+
3
5
  /**
4
6
  * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.
5
7
  *
@@ -81,10 +83,15 @@ const defaultJsonValueReplacer = (key, value) => {
81
83
  const asJson = (value, replacer = defaultJsonValueReplacer, space) => {
82
84
  return JSON.stringify(value, replacer, space);
83
85
  };
86
+ /** Calculate minimum number of extra program pages required for provided approval and clear state programs */
87
+ const calculateExtraProgramPages = (approvalProgram, clearStateProgram) => {
88
+ return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / types_app.APP_PAGE_MAX_SIZE);
89
+ };
84
90
 
85
91
  exports.UnsafeConversionError = UnsafeConversionError;
86
92
  exports.asJson = asJson;
87
93
  exports.binaryStartsWith = binaryStartsWith;
94
+ exports.calculateExtraProgramPages = calculateExtraProgramPages;
88
95
  exports.calculateFundAmount = calculateFundAmount;
89
96
  exports.chunkArray = chunkArray;
90
97
  exports.defaultJsonValueReplacer = defaultJsonValueReplacer;
package/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sources":["../src/util.ts"],"sourcesContent":["/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n"],"names":[],"mappings":";;AAAA;;;;;AAKG;AACU,MAAA,QAAQ,GAAG,CAAC,KAAsB,KAAI;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,KAAK;IAE3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,oFAAA,CAAsF,CAC9G;;SACI,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAClD,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,qFAAA,CAAuF,CAC/G;;AAEH,IAAA,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB;AAEM,MAAO,qBAAsB,SAAQ,KAAK,CAAA;AAAG;AAEnD;;;;;;AAMG;AACU,MAAA,mBAAmB,GAAG,CACjC,kBAA0B,EAC1B,sBAA8B,EAC9B,mBAA2B,KACV;AACjB,IAAA,IAAI,kBAAkB,GAAG,sBAAsB,EAAE;AAC/C,QAAA,MAAM,aAAa,GAAG,kBAAkB,GAAG,sBAAsB;AACjE,QAAA,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC,mBAAmB,CAAC,CAAC,CAAC;;SACtE;AACL,QAAA,OAAO,IAAI;;AAEf;AAWA;;;;;AAKG;UACc,UAAU,CAAI,KAAU,EAAE,SAAiB,EAAA;AAC1D,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,SAAS;QAAE,MAAM,KAAK,CAAC,KAAK,CAAC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AAC,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC;AAC/C;;;;;;;;;;;"}
1
+ {"version":3,"file":"util.js","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n"],"names":["APP_PAGE_MAX_SIZE"],"mappings":";;;;AAEA;;;;;AAKG;AACU,MAAA,QAAQ,GAAG,CAAC,KAAsB,KAAI;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,KAAK;IAE3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,oFAAA,CAAsF,CAC9G;;SACI,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAClD,QAAA,MAAM,IAA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AAgB,GAAG,CAAC,IAAgB,EAAE,UAAsB,KAAa;AACpF,IAAA,IAAI,UAAU,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM;AAAE,QAAA,OAAO,KAAK;AACjD,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC;AAAE,YAAA,OAAO,KAAK;;AAE7C,IAAA,OAAO,IAAI;AACb;MAEa,wBAAwB,GAAG,CAAC,GAAW,EAAE,KAAc,KAAI;AACtE,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,IAAI;AACF,YAAA,OAAO,QAAQ,CAAC,KAAK,CAAC;;AACtB,QAAA,MAAM;AACN,YAAA,OAAO,KAAK,CAAC,QAAQ,EAAE;;;AAG3B,IAAA,OAAO,KAAK;AACd;AACO,MAAM,MAAM,GAAG,CACpB,KAAc,EACd,QAAA,GAAqD,wBAAwB,EAC7E,KAAuB,KACrB;IACF,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC;AAC/C;AAEA;MACa,0BAA0B,GAAG,CAAC,eAA2B,EAAE,iBAA8B,KAAY;IAChH,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,eAAe,CAAC,MAAM,IAAI,iBAAiB,EAAE,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,IAAIA,2BAAiB,CAAC;AACxG;;;;;;;;;;;;"}
package/util.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { APP_PAGE_MAX_SIZE } from './types/app.mjs';
2
+
1
3
  /**
2
4
  * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.
3
5
  *
@@ -79,6 +81,10 @@ const defaultJsonValueReplacer = (key, value) => {
79
81
  const asJson = (value, replacer = defaultJsonValueReplacer, space) => {
80
82
  return JSON.stringify(value, replacer, space);
81
83
  };
84
+ /** Calculate minimum number of extra program pages required for provided approval and clear state programs */
85
+ const calculateExtraProgramPages = (approvalProgram, clearStateProgram) => {
86
+ return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE);
87
+ };
82
88
 
83
- export { UnsafeConversionError, asJson, binaryStartsWith, calculateFundAmount, chunkArray, defaultJsonValueReplacer, memoize, toNumber };
89
+ export { UnsafeConversionError, asJson, binaryStartsWith, calculateExtraProgramPages, calculateFundAmount, chunkArray, defaultJsonValueReplacer, memoize, toNumber };
84
90
  //# sourceMappingURL=util.mjs.map
package/util.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"util.mjs","sources":["../src/util.ts"],"sourcesContent":["/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n"],"names":[],"mappings":"AAAA;;;;;AAKG;AACU,MAAA,QAAQ,GAAG,CAAC,KAAsB,KAAI;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,KAAK;IAE3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAk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K,CAAC,GAAG,CAAC,GAAG,CAAC;AAC/F,KAAC;AACD,IAAA,MAAM,CAAC,KAAK,GAAG,KAAK;AACpB,IAAA,OAAO,MAAuB;AAChC;MAEa,gBAAgB,GAAG,CAAC,IAAgB,EAAE,UAAsB,KAAa;AACpF,IAAA,IAAI,UAAU,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM;AAAE,QAAA,OAAO,KAAK;AACjD,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAC1C,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC;AAAE,YAAA,OAAO,KAAK;;AAE7C,IAAA,OAAO,IAAI;AACb;MAEa,wBAAwB,GAAG,CAAC,GAAW,EAAE,KAAc,KAAI;AACtE,IAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;AAC7B,QAAA,IAAI;AACF,YAAA,OAAO,QAAQ,CAAC,KAAK,CAAC;;AACtB,QAAA,MAAM;AACN,YAAA,OAAO,KAAK,CAAC,QAAQ,EAAE;;;AAG3B,IAAA,OAAO,KAAK;AACd;AACO,MAAM,MAAM,GAAG,CACpB,KAAc,EACd,QAAA,GAAqD,wBAAwB,EAC7E,KAAuB,KACrB;IACF,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,QAAQ,EAAE,KAAK,CAAC;AAC/C;;;;"}
1
+ {"version":3,"file":"util.mjs","sources":["../src/util.ts"],"sourcesContent":["import { APP_PAGE_MAX_SIZE } from './types/app'\n\n/**\n * Converts a value which might be a number or a bigint into a number to be used with apis that don't support bigint.\n *\n * Throws an UnsafeConversionError if the conversion would result in an unsafe integer for the Number type\n * @param value\n */\nexport const toNumber = (value: number | bigint) => {\n if (typeof value === 'number') return value\n\n if (value > BigInt(Number.MAX_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is larger than the maximum safe integer the Number type can hold.`,\n )\n } else if (value < BigInt(Number.MIN_SAFE_INTEGER)) {\n throw new UnsafeConversionError(\n `Cannot convert ${value} to a Number as it is smaller than the minimum safe integer the Number type can hold.`,\n )\n }\n return Number(value)\n}\n\nexport class UnsafeConversionError extends Error {}\n\n/**\n * Calculates the amount of funds to add to a wallet to bring it up to the minimum spending balance.\n * @param minSpendingBalance The minimum spending balance for the wallet\n * @param currentSpendingBalance The current spending balance for the wallet\n * @param minFundingIncrement The minimum amount of funds that can be added to the wallet\n * @returns The amount of funds to add to the wallet or null if the wallet is already above the minimum spending balance\n */\nexport const calculateFundAmount = (\n minSpendingBalance: bigint,\n currentSpendingBalance: bigint,\n minFundingIncrement: bigint,\n): bigint | null => {\n if (minSpendingBalance > currentSpendingBalance) {\n const minFundAmount = minSpendingBalance - currentSpendingBalance\n return BigInt(Math.max(Number(minFundAmount), Number(minFundingIncrement)))\n } else {\n return null\n }\n}\n\n/**\n * Checks if the current environment is Node.js\n *\n * @returns A boolean indicating whether the current environment is Node.js\n */\nexport const isNode = () => {\n return typeof process !== 'undefined' && process.versions != null && process.versions.node != null\n}\n\n/**\n * Returns the given array split into chunks of `batchSize` batches.\n * @param array The array to chunk\n * @param batchSize The size of batches to split the array into\n * @returns A generator that yields the array split into chunks of `batchSize` batches\n */\nexport function* chunkArray<T>(array: T[], batchSize: number): Generator<T[], void> {\n for (let i = 0; i < array.length; i += batchSize) yield array.slice(i, i + batchSize)\n}\n\n/**\n * Memoize calls to the given function in an in-memory map.\n * @param fn The function to memoize\n * @returns The memoized function\n */\nexport const memoize = <T = unknown, R = unknown>(fn: (val: T) => R) => {\n const cache = new Map()\n const cached = function (this: unknown, val: T) {\n return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)\n }\n cached.cache = cache\n return cached as (val: T) => R\n}\n\nexport const binaryStartsWith = (base: Uint8Array, startsWith: Uint8Array): boolean => {\n if (startsWith.length > base.length) return false\n for (let i = 0; i < startsWith.length; i++) {\n if (base[i] !== startsWith[i]) return false\n }\n return true\n}\n\nexport const defaultJsonValueReplacer = (key: string, value: unknown) => {\n if (typeof value === 'bigint') {\n try {\n return toNumber(value)\n } catch {\n return value.toString()\n }\n }\n return value\n}\nexport const asJson = (\n value: unknown,\n replacer: (key: string, value: unknown) => unknown = defaultJsonValueReplacer,\n space?: string | number,\n) => {\n return JSON.stringify(value, replacer, space)\n}\n\n/** Calculate minimum number of extra program pages required for provided approval and clear state programs */\nexport const calculateExtraProgramPages = (approvalProgram: Uint8Array, clearStateProgram?: Uint8Array): number => {\n return Math.floor((approvalProgram.length + (clearStateProgram?.length ?? 0) - 1) / APP_PAGE_MAX_SIZE)\n}\n"],"names":[],"mappings":";;AAEA;;;;;AAKG;AACU,MAAA,QAAQ,GAAG,CAAC,KAAsB,KAAI;IACjD,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,KAAK;IAE3C,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAC3C,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,oFAAA,CAAsF,CAC9G;;SACI,IAAI,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE;AAClD,QAAA,MAAM,IAAI,qBAAqB,CAC7B,kBAAkB,KAAK,CAAA,qFAAA,CAAuF,CAC/G;;AAEH,IAAA,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB;AAEM,MAAO,qBAAsB,SAAQ,KAAK,CAAA;AAAG;AAEnD;;;;;;AAMG;AACU,MAAA,mBAAmB,GAAG,CACjC,kBAA0B,EAC1B,sBAA8B,EAC9B,mBAA2B,KACV;AACjB,IAAA,IAAI,kBAAkB,GAAG,sBAAsB,EAAE;AAC/C,QAAA,MAAM,aAAa,GAAG,kBAAkB,GAAG,sBAAsB;AACjE,QAAA,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CA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