@kikiutils/shared 14.0.1 → 14.1.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/dist/buffer.d.ts +18 -19
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js +22 -20
- package/dist/buffer.js.map +1 -1
- package/dist/datetime.js.map +1 -1
- package/dist/object.js.map +1 -1
- package/dist/string.js.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +22 -26
- package/src/buffer.ts +23 -20
- package/src/types/index.ts +1 -1
package/dist/buffer.d.ts
CHANGED
|
@@ -4,34 +4,33 @@ import { Buffer } from "node:buffer";
|
|
|
4
4
|
//#region src/buffer.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Converts
|
|
7
|
+
* Converts various binary data types to a Node.js Buffer.
|
|
8
8
|
*
|
|
9
|
-
* This function provides a unified way to convert
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* This function provides a unified, efficient way to convert different binary formats
|
|
10
|
+
* (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
|
|
11
|
+
* It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
|
|
12
|
+
* optimal performance.
|
|
12
13
|
*
|
|
13
|
-
* @param {Blob | Buffer | File} input - The input to convert
|
|
14
|
+
* @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
|
|
15
|
+
* Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
|
|
14
16
|
*
|
|
15
|
-
* @returns {Promise<Buffer>} A
|
|
17
|
+
* @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
|
|
16
18
|
*
|
|
17
19
|
* @example
|
|
18
20
|
* ```typescript
|
|
19
|
-
* import { toBuffer } from '@kikiutils/shared/
|
|
21
|
+
* import { toBuffer } from '@kikiutils/shared/buffer';
|
|
20
22
|
*
|
|
21
|
-
* //
|
|
22
|
-
* const
|
|
23
|
-
* const
|
|
24
|
-
* console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
|
|
23
|
+
* // From ArrayBuffer
|
|
24
|
+
* const ab = new ArrayBuffer(8);
|
|
25
|
+
* const bufferFromAB = await toBuffer(ab);
|
|
25
26
|
*
|
|
26
|
-
* //
|
|
27
|
-
* const
|
|
28
|
-
* const
|
|
29
|
-
* console.log(result2.toString()); // 'Hello from Blob'
|
|
27
|
+
* // From Uint8Array (Zero-copy)
|
|
28
|
+
* const u8 = new Uint8Array([10, 20, 30]);
|
|
29
|
+
* const bufferFromU8 = await toBuffer(u8);
|
|
30
30
|
*
|
|
31
|
-
* //
|
|
32
|
-
* const
|
|
33
|
-
* const
|
|
34
|
-
* console.log(result3.toString()); // 'File content'
|
|
31
|
+
* // From Blob or File
|
|
32
|
+
* const blob = new Blob(['data'], { type: 'text/plain' });
|
|
33
|
+
* const bufferFromBlob = await toBuffer(blob);
|
|
35
34
|
* ```
|
|
36
35
|
*/
|
|
37
36
|
declare function toBuffer(input: BinaryInput): Promise<Buffer<ArrayBufferLike>>;
|
package/dist/buffer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buffer.d.ts","names":[],"sources":["../src/buffer.ts"],"sourcesContent":[],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"file":"buffer.d.ts","names":[],"sources":["../src/buffer.ts"],"sourcesContent":[],"mappings":";;;;;;;AAkCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAsB,QAAA,QAAgB,cAAW,QAAA,OAAA"}
|
package/dist/buffer.js
CHANGED
|
@@ -2,39 +2,41 @@ import { Buffer } from "node:buffer";
|
|
|
2
2
|
|
|
3
3
|
//#region src/buffer.ts
|
|
4
4
|
/**
|
|
5
|
-
* Converts
|
|
5
|
+
* Converts various binary data types to a Node.js Buffer.
|
|
6
6
|
*
|
|
7
|
-
* This function provides a unified way to convert
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* This function provides a unified, efficient way to convert different binary formats
|
|
8
|
+
* (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
|
|
9
|
+
* It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
|
|
10
|
+
* optimal performance.
|
|
10
11
|
*
|
|
11
|
-
* @param {Blob | Buffer | File} input - The input to convert
|
|
12
|
+
* @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
|
|
13
|
+
* Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
|
|
12
14
|
*
|
|
13
|
-
* @returns {Promise<Buffer>} A
|
|
15
|
+
* @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
|
|
14
16
|
*
|
|
15
17
|
* @example
|
|
16
18
|
* ```typescript
|
|
17
|
-
* import { toBuffer } from '@kikiutils/shared/
|
|
19
|
+
* import { toBuffer } from '@kikiutils/shared/buffer';
|
|
18
20
|
*
|
|
19
|
-
* //
|
|
20
|
-
* const
|
|
21
|
-
* const
|
|
22
|
-
* console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
|
|
21
|
+
* // From ArrayBuffer
|
|
22
|
+
* const ab = new ArrayBuffer(8);
|
|
23
|
+
* const bufferFromAB = await toBuffer(ab);
|
|
23
24
|
*
|
|
24
|
-
* //
|
|
25
|
-
* const
|
|
26
|
-
* const
|
|
27
|
-
* console.log(result2.toString()); // 'Hello from Blob'
|
|
25
|
+
* // From Uint8Array (Zero-copy)
|
|
26
|
+
* const u8 = new Uint8Array([10, 20, 30]);
|
|
27
|
+
* const bufferFromU8 = await toBuffer(u8);
|
|
28
28
|
*
|
|
29
|
-
* //
|
|
30
|
-
* const
|
|
31
|
-
* const
|
|
32
|
-
* console.log(result3.toString()); // 'File content'
|
|
29
|
+
* // From Blob or File
|
|
30
|
+
* const blob = new Blob(['data'], { type: 'text/plain' });
|
|
31
|
+
* const bufferFromBlob = await toBuffer(blob);
|
|
33
32
|
* ```
|
|
34
33
|
*/
|
|
35
34
|
async function toBuffer(input) {
|
|
36
35
|
if (Buffer.isBuffer(input)) return input;
|
|
37
|
-
return Buffer.from(
|
|
36
|
+
if (input instanceof ArrayBuffer) return Buffer.from(input);
|
|
37
|
+
if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
38
|
+
if (typeof input.arrayBuffer === "function") return Buffer.from(await input.arrayBuffer());
|
|
39
|
+
throw new TypeError("The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).");
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
//#endregion
|
package/dist/buffer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buffer.js","names":[],"sources":["../src/buffer.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\n\nimport type { BinaryInput } from './types';\n\n/**\n * Converts
|
|
1
|
+
{"version":3,"file":"buffer.js","names":[],"sources":["../src/buffer.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\n\nimport type { BinaryInput } from './types';\n\n/**\n * Converts various binary data types to a Node.js Buffer.\n *\n * This function provides a unified, efficient way to convert different binary formats\n * (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.\n * It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure\n * optimal performance.\n *\n * @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.\n * Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.\n *\n * @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.\n *\n * @example\n * ```typescript\n * import { toBuffer } from '@kikiutils/shared/buffer';\n *\n * // From ArrayBuffer\n * const ab = new ArrayBuffer(8);\n * const bufferFromAB = await toBuffer(ab);\n *\n * // From Uint8Array (Zero-copy)\n * const u8 = new Uint8Array([10, 20, 30]);\n * const bufferFromU8 = await toBuffer(u8);\n *\n * // From Blob or File\n * const blob = new Blob(['data'], { type: 'text/plain' });\n * const bufferFromBlob = await toBuffer(blob);\n * ```\n */\nexport async function toBuffer(input: BinaryInput) {\n if (Buffer.isBuffer(input)) return input;\n if (input instanceof ArrayBuffer) return Buffer.from(input);\n if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);\n if (typeof input.arrayBuffer === 'function') return Buffer.from(await input.arrayBuffer());\n // eslint-disable-next-line style/max-len\n throw new TypeError('The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).');\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCA,eAAsB,SAAS,OAAoB;AAC/C,KAAI,OAAO,SAAS,MAAM,CAAE,QAAO;AACnC,KAAI,iBAAiB,YAAa,QAAO,OAAO,KAAK,MAAM;AAC3D,KAAI,iBAAiB,WAAY,QAAO,OAAO,KAAK,MAAM,QAAQ,MAAM,YAAY,MAAM,WAAW;AACrG,KAAI,OAAO,MAAM,gBAAgB,WAAY,QAAO,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAE1F,OAAM,IAAI,UAAU,sGAAsG"}
|
package/dist/datetime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"datetime.js","names":["dateFnsFormat","format"
|
|
1
|
+
{"version":3,"file":"datetime.js","names":["dateFnsFormat","format"],"sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string\n * @param {string} [format] - The target format string\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`\n *\n * @returns {string} The formatted date string\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date\n * @param {DateRangeType} type - The range type to compute\n * @param {object} [options] - Optional settings\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)\n *\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative\n *\n * @returns {Date} A `Date` object at 00:00:00 of the offset day\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,SAAgB,WAAW,MAA0B,WAAiB,uBAAuB,SAAyB;AAClH,QAAOA,OAAc,MAAMC,UAAQ,QAAQ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8B/C,SAAgB,qBACZ,MACA,MACA,SAIF;CACE,IAAI;CACJ,IAAI;AACJ,SAAQ,MAAR;EACI,KAAK;GACD;IACI,MAAM,YAAY,UAAU,MAAM,EAAE;AACpC,cAAU,WAAW,UAAU;AAC/B,gBAAY,aAAa,UAAU;;AAGvC;EACJ,KAAK;GACD;IACI,MAAM,WAAW,SAAS,MAAM,EAAE;AAClC,cAAU,UAAU,UAAU,EAAE,cAAc,SAAS,gBAAgB,GAAG,CAAC;AAC3E,gBAAY,YAAY,UAAU,EAAE,cAAc,SAAS,gBAAgB,GAAG,CAAC;;AAGnF;EACJ,KAAK;AACD,aAAU,WAAW,KAAK;AAC1B,eAAY,aAAa,KAAK;AAC9B;EACJ,KAAK;AACD,aAAU,UAAU,MAAM,EAAE,cAAc,SAAS,gBAAgB,GAAG,CAAC;AACvE,eAAY,YAAY,MAAM,EAAE,cAAc,SAAS,gBAAgB,GAAG,CAAC;AAC3E;EACJ,KAAK;AACD,aAAU,SAAS,KAAK;AACxB,eAAY,WAAW,KAAK;AAC5B;EACJ,KAAK;GACD;IACI,MAAM,YAAY,QAAQ,MAAM,EAAE;AAClC,cAAU,SAAS,UAAU;AAC7B,gBAAY,WAAW,UAAU;;AAGrC;EACJ,QAAS,OAAM,IAAI,MAAM,gCAAgC,OAAO;;AAGpE,KAAI,SAAS,yBAA0B,SAAQ,SAAS,IAAI,GAAG,GAAG,EAAE;AACpE,QAAO;EACH;EACA;EACH;;;;;;;;;;;;;;;;;;AAmBL,SAAgB,yBAAyB,aAAqB,GAAG;CAC7D,MAAM,uBAAO,IAAI,MAAM;AACvB,MAAK,QAAQ,KAAK,SAAS,GAAG,WAAW;AACzC,MAAK,SAAS,GAAG,GAAG,GAAG,EAAE;AACzB,QAAO"}
|
package/dist/object.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.js","names":[
|
|
1
|
+
{"version":3,"file":"object.js","names":[],"sources":["../src/object.ts"],"sourcesContent":["/**\n * Serializes a nested object into a deterministic, flat string format.\n *\n * This function recursively traverses the input object (including nested objects and arrays),\n * flattens it into key paths using dot notation (e.g. \"a.b.0.c\"), sorts all keys,\n * and joins each key-value pair into a string with customizable separators.\n *\n * It is designed for use cases such as signature generation, cache key construction,\n * or any context requiring consistent and predictable object serialization.\n *\n * @param {Record<string, any>} input - The object to serialize. Can contain nested objects and arrays\n * @param {string} kvSeparator - The string used to separate each key from its value (default: '=')\n * @param {string} pairSeparator - The string used to separate each key-value pair (default: '&')\n * @returns {string} A deterministic string representation of the input object\n *\n * @example\n * ```typescript\n * import { stringifyObjectDeterministically } from '@kikiutils/shared/object';\n *\n * console.log(stringifyObjectDeterministically({ b: 2, a: { x: 1, y: [3, 4] } })); // a.x=1&a.y.0=3&a.y.1=4&b=2\n * console.log(stringifyObjectDeterministically({ foo: 'bar' }, ':', '|')); // foo:bar\n * ```\n */\nexport function stringifyObjectDeterministically(\n input: Record<string, any>,\n kvSeparator: string = '=',\n pairSeparator: string = '&',\n) {\n const entries: string[] = [];\n\n function walk(object: any, path: string[] = []) {\n if (Array.isArray(object)) {\n object.forEach((value, i) => {\n walk(\n value,\n [\n ...path,\n i.toString(),\n ],\n );\n });\n } else if (\n object !== null\n && typeof object === 'object'\n && Object.prototype.toString.call(object) === '[object Object]'\n ) {\n Object.keys(object).sort().forEach((key) => {\n walk(\n object[key],\n [\n ...path,\n key,\n ],\n );\n });\n } else entries.push(`${path.join('.')}${kvSeparator}${String(object)}`);\n }\n\n walk(input);\n return entries.sort().join(pairSeparator);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,iCACZ,OACA,cAAsB,KACtB,gBAAwB,KAC1B;CACE,MAAM,UAAoB,EAAE;CAE5B,SAAS,KAAK,QAAa,OAAiB,EAAE,EAAE;AAC5C,MAAI,MAAM,QAAQ,OAAO,CACrB,QAAO,SAAS,OAAO,MAAM;AACzB,QACI,OACA,CACI,GAAG,MACH,EAAE,UAAU,CACf,CACJ;IACH;WAEF,WAAW,QACR,OAAO,WAAW,YAClB,OAAO,UAAU,SAAS,KAAK,OAAO,KAAK,kBAE9C,QAAO,KAAK,OAAO,CAAC,MAAM,CAAC,SAAS,QAAQ;AACxC,QACI,OAAO,MACP,CACI,GAAG,MACH,IACH,CACJ;IACH;MACC,SAAQ,KAAK,GAAG,KAAK,KAAK,IAAI,GAAG,cAAc,OAAO,OAAO,GAAG;;AAG3E,MAAK,MAAM;AACX,QAAO,QAAQ,MAAM,CAAC,KAAK,cAAc"}
|
package/dist/string.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.js","names":[
|
|
1
|
+
{"version":3,"file":"string.js","names":[],"sources":["../src/string.ts"],"sourcesContent":["export type RandomStringMode =\n | 'alphabetic'\n | 'alphanumeric'\n | 'lowercase'\n | 'lowercase-numeric'\n | 'numeric'\n | 'uppercase'\n | 'uppercase-numeric';\n\nconst DIGITS = '0123456789';\nconst LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';\nconst UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst CHARSETS: Record<RandomStringMode, string> = {\n 'alphabetic': LOWERCASE + UPPERCASE,\n 'alphanumeric': DIGITS + LOWERCASE + UPPERCASE,\n 'lowercase': LOWERCASE,\n 'lowercase-numeric': DIGITS + LOWERCASE,\n 'numeric': DIGITS,\n 'uppercase': UPPERCASE,\n 'uppercase-numeric': DIGITS + UPPERCASE,\n};\n\n/**\n * Generates a random string of a given length using a specified character set.\n *\n * @param {number} length - The length of the string to generate. Must be a positive integer\n * @param {RandomStringMode} [mode] - The character set to use\n *\n * @returns {string} The generated random string\n *\n * @throws {Error} If the length is not a positive integer or the mode is unsupported\n *\n * @example\n * ```typescript\n * import { randomString } from '@kikiutils/shared/string';\n *\n * console.log(randomString(8)); // e.g. 'aZbXwTyQ' (alphabetic)\n * console.log(randomString(6, 'numeric')); // e.g. '402398'\n * console.log(randomString(10, 'alphanumeric')); // e.g. 'a9Z4pQ8xY2'\n * ```\n */\nexport function randomString(length: number, mode: RandomStringMode = 'alphabetic') {\n if (!Number.isInteger(length) || length <= 0) {\n throw new Error(`Invalid length: ${length}. Must be a positive integer`);\n }\n\n const charset = CHARSETS[mode];\n if (!charset) throw new Error(`Unsupported mode: ${mode}`);\n return Array.from({ length }, () => charset[Math.floor(Math.random() * charset.length)]).join('');\n}\n"],"mappings":";AASA,MAAM,SAAS;AACf,MAAM,YAAY;AAClB,MAAM,YAAY;AAClB,MAAM,WAA6C;CAC/C,cAAc,YAAY;CAC1B,gBAAgB,SAAS,YAAY;CACrC,aAAa;CACb,qBAAqB,SAAS;CAC9B,WAAW;CACX,aAAa;CACb,qBAAqB,SAAS;CACjC;;;;;;;;;;;;;;;;;;;;AAqBD,SAAgB,aAAa,QAAgB,OAAyB,cAAc;AAChF,KAAI,CAAC,OAAO,UAAU,OAAO,IAAI,UAAU,EACvC,OAAM,IAAI,MAAM,mBAAmB,OAAO,8BAA8B;CAG5E,MAAM,UAAU,SAAS;AACzB,KAAI,CAAC,QAAS,OAAM,IAAI,MAAM,qBAAqB,OAAO;AAC1D,QAAO,MAAM,KAAK,EAAE,QAAQ,QAAQ,QAAQ,KAAK,MAAM,KAAK,QAAQ,GAAG,QAAQ,OAAO,EAAE,CAAC,KAAK,GAAG"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Blob as Blob$1, Buffer, File as File$1 } from "node:buffer";
|
|
|
3
3
|
|
|
4
4
|
//#region src/types/index.d.ts
|
|
5
5
|
type AnyRecord = Record<string, any>;
|
|
6
|
-
type BinaryInput = Blob | Buffer | File | Blob$1 | File$1;
|
|
6
|
+
type BinaryInput = ArrayBuffer | Blob | Buffer | File | Blob$1 | File$1 | Uint8Array;
|
|
7
7
|
type Booleanish = 'false' | 'true' | boolean;
|
|
8
8
|
type MaybePartial<T> = Partial<T> | T;
|
|
9
9
|
type MaybeReadonly<T> = Readonly<T> | T;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"sourcesContent":[],"mappings":";;;;KAQY,SAAA,GAAY;KACZ,WAAA,GAAc,OAAO,SAAS,OAAO,SAAW;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"sourcesContent":[],"mappings":";;;;KAQY,SAAA,GAAY;KACZ,WAAA,GAAc,cAAc,OAAO,SAAS,OAAO,SAAW,SAAW;AADzE,KAEA,UAAA,GAFS,OAAG,GAAM,MAAA,GAAA,OAAA;AAClB,KAEA,YAFW,CAAA,CAAA,CAAA,GAEO,OAFP,CAEe,CAFf,CAAA,GAEoB,CAFpB;AAAG,KAGd,aAHc,CAAA,CAAA,CAAA,GAGK,QAHL,CAGc,CAHd,CAAA,GAGmB,CAHnB;AAAc,KAI5B,QAJ4B,CAAA,CAAA,CAAA,GAAA,IAAA,GAIP,CAJO;AAAO,KAKnC,SAAA,GALmC,MAAA,GAAA,MAAA;AAAS,KAM5C,aAN4C,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAMJ,OANI,CAMI,MANJ,CAMW,CANX,EAMc,CANd,CAAA,CAAA;AAAO,KAOnD,qBAPmD,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAOH,QAPG,CAOM,aAPN,CAOoB,CAPpB,EAOuB,CAPvB,CAAA,CAAA;AAAW,KAQ9D,cAR8D,CAAA,UAAA,MAAA,GAAA,EAAA,CAAA,CAAA,GAQrB,QARqB,CAQZ,MARY,CAQL,CARK,EAQF,CARE,CAAA,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "14.
|
|
4
|
+
"version": "14.1.1",
|
|
5
5
|
"description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
@@ -33,7 +33,6 @@
|
|
|
33
33
|
],
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"exports": {
|
|
36
|
-
"./*": "./dist/*.js",
|
|
37
36
|
"./package.json": "./package.json",
|
|
38
37
|
"./types": {
|
|
39
38
|
"types": "./dist/types/index.d.ts",
|
|
@@ -44,7 +43,8 @@
|
|
|
44
43
|
"types": null,
|
|
45
44
|
"import": null,
|
|
46
45
|
"require": null
|
|
47
|
-
}
|
|
46
|
+
},
|
|
47
|
+
"./*": "./dist/*.js"
|
|
48
48
|
},
|
|
49
49
|
"files": [
|
|
50
50
|
"./dist",
|
|
@@ -67,26 +67,25 @@
|
|
|
67
67
|
"unused-exports": "ts-unused-exports ./tsconfig.json"
|
|
68
68
|
},
|
|
69
69
|
"peerDependencies": {
|
|
70
|
-
"@noble/hashes": "^2.0.
|
|
70
|
+
"@noble/hashes": "^2.0.1",
|
|
71
71
|
"@types/fs-extra": "^11.0.4",
|
|
72
72
|
"@types/jsonfile": "^6.1.4",
|
|
73
|
-
"@types/node": ">= 24",
|
|
74
73
|
"@types/ssh2": "^1.15.5",
|
|
75
74
|
"async-validator": "^4.2.5",
|
|
76
|
-
"bson": "^7.
|
|
75
|
+
"bson": "^7.1.1",
|
|
77
76
|
"consola": "^3.4.2",
|
|
78
77
|
"date-fns": "^4.1.0",
|
|
79
78
|
"decimal.js": "^10.6.0",
|
|
80
|
-
"element-plus": "^2.
|
|
81
|
-
"fs-extra": "^11.3.
|
|
79
|
+
"element-plus": "^2.13.1",
|
|
80
|
+
"fs-extra": "^11.3.3",
|
|
82
81
|
"lru-cache": "^11.2.4",
|
|
83
82
|
"millify": "^6.1.0",
|
|
84
|
-
"msgpackr": "^1.11.
|
|
83
|
+
"msgpackr": "^1.11.8",
|
|
85
84
|
"node-ssh": "^13.2.1",
|
|
86
|
-
"pino": "^10.
|
|
87
|
-
"pino-pretty": "^13.1.
|
|
88
|
-
"vue": "^3.5.
|
|
89
|
-
"vue-router": "^4.
|
|
85
|
+
"pino": "^10.2.0",
|
|
86
|
+
"pino-pretty": "^13.1.3",
|
|
87
|
+
"vue": "^3.5.26",
|
|
88
|
+
"vue-router": "^4.6.4"
|
|
90
89
|
},
|
|
91
90
|
"peerDependenciesMeta": {
|
|
92
91
|
"@noble/hashes": {
|
|
@@ -98,9 +97,6 @@
|
|
|
98
97
|
"@types/jsonfile": {
|
|
99
98
|
"optional": true
|
|
100
99
|
},
|
|
101
|
-
"@types/node": {
|
|
102
|
-
"optional": true
|
|
103
|
-
},
|
|
104
100
|
"@types/ssh2": {
|
|
105
101
|
"optional": true
|
|
106
102
|
},
|
|
@@ -151,38 +147,38 @@
|
|
|
151
147
|
}
|
|
152
148
|
},
|
|
153
149
|
"devDependencies": {
|
|
154
|
-
"@antfu/eslint-config": "^
|
|
155
|
-
"@kikiutils/eslint-config": "^5.0.
|
|
150
|
+
"@antfu/eslint-config": "^7.0.1",
|
|
151
|
+
"@kikiutils/eslint-config": "^5.0.2",
|
|
156
152
|
"@kikiutils/tsconfigs": "^5.0.5",
|
|
157
153
|
"@noble/hashes": "^2.0.1",
|
|
158
154
|
"@types/fs-extra": "^11.0.4",
|
|
159
155
|
"@types/jsonfile": "^6.1.4",
|
|
160
|
-
"@types/node": "^25.0.
|
|
156
|
+
"@types/node": "^25.0.9",
|
|
161
157
|
"@types/ssh2": "^1.15.5",
|
|
162
|
-
"@vitest/coverage-v8": "^4.0.
|
|
158
|
+
"@vitest/coverage-v8": "^4.0.17",
|
|
163
159
|
"async-validator": "^4.2.5",
|
|
164
|
-
"bson": "^7.
|
|
160
|
+
"bson": "^7.1.1",
|
|
165
161
|
"changelogen": "^0.6.2",
|
|
166
162
|
"consola": "^3.4.2",
|
|
167
163
|
"cross-env": "^10.1.0",
|
|
168
164
|
"date-fns": "^4.1.0",
|
|
169
165
|
"decimal.js": "^10.6.0",
|
|
170
166
|
"depcheck": "^1.4.7",
|
|
171
|
-
"element-plus": "^2.13.
|
|
167
|
+
"element-plus": "^2.13.1",
|
|
172
168
|
"fs-extra": "^11.3.3",
|
|
173
|
-
"jsdom": "^27.
|
|
169
|
+
"jsdom": "^27.4.0",
|
|
174
170
|
"lru-cache": "^11.2.4",
|
|
175
171
|
"millify": "^6.1.0",
|
|
176
172
|
"msgpackr": "^1.11.8",
|
|
177
173
|
"node-ssh": "^13.2.1",
|
|
178
|
-
"pino": "^10.
|
|
174
|
+
"pino": "^10.2.0",
|
|
179
175
|
"pino-pretty": "^13.1.3",
|
|
180
176
|
"publint": "^0.3.16",
|
|
181
177
|
"ts-unused-exports": "^11.0.1",
|
|
182
|
-
"tsdown": "^0.
|
|
178
|
+
"tsdown": "^0.19.0",
|
|
183
179
|
"typescript": "^5.9.3",
|
|
184
180
|
"unplugin-unused": "^0.5.6",
|
|
185
|
-
"vitest": "^4.0.
|
|
181
|
+
"vitest": "^4.0.17",
|
|
186
182
|
"vue": "^3.5.26",
|
|
187
183
|
"vue-router": "^4.6.4"
|
|
188
184
|
},
|
package/src/buffer.ts
CHANGED
|
@@ -3,37 +3,40 @@ import { Buffer } from 'node:buffer';
|
|
|
3
3
|
import type { BinaryInput } from './types';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Converts
|
|
6
|
+
* Converts various binary data types to a Node.js Buffer.
|
|
7
7
|
*
|
|
8
|
-
* This function provides a unified way to convert
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* This function provides a unified, efficient way to convert different binary formats
|
|
9
|
+
* (Blob, Buffer, File, ArrayBuffer, or Uint8Array) into a Node.js Buffer.
|
|
10
|
+
* It prioritizes zero-copy conversions for TypedArrays and ArrayBuffers to ensure
|
|
11
|
+
* optimal performance.
|
|
11
12
|
*
|
|
12
|
-
* @param {Blob | Buffer | File} input - The input to convert
|
|
13
|
+
* @param {ArrayBuffer | Blob | Buffer | File | Uint8Array} input - The binary data input to convert.
|
|
14
|
+
* Supports Blob, Buffer, File, ArrayBuffer, and Uint8Array.
|
|
13
15
|
*
|
|
14
|
-
* @returns {Promise<Buffer>} A
|
|
16
|
+
* @returns {Promise<Buffer>} A promise that resolves to a Node.js Buffer.
|
|
15
17
|
*
|
|
16
18
|
* @example
|
|
17
19
|
* ```typescript
|
|
18
|
-
* import { toBuffer } from '@kikiutils/shared/
|
|
20
|
+
* import { toBuffer } from '@kikiutils/shared/buffer';
|
|
19
21
|
*
|
|
20
|
-
* //
|
|
21
|
-
* const
|
|
22
|
-
* const
|
|
23
|
-
* console.log(result1); // <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>
|
|
22
|
+
* // From ArrayBuffer
|
|
23
|
+
* const ab = new ArrayBuffer(8);
|
|
24
|
+
* const bufferFromAB = await toBuffer(ab);
|
|
24
25
|
*
|
|
25
|
-
* //
|
|
26
|
-
* const
|
|
27
|
-
* const
|
|
28
|
-
* console.log(result2.toString()); // 'Hello from Blob'
|
|
26
|
+
* // From Uint8Array (Zero-copy)
|
|
27
|
+
* const u8 = new Uint8Array([10, 20, 30]);
|
|
28
|
+
* const bufferFromU8 = await toBuffer(u8);
|
|
29
29
|
*
|
|
30
|
-
* //
|
|
31
|
-
* const
|
|
32
|
-
* const
|
|
33
|
-
* console.log(result3.toString()); // 'File content'
|
|
30
|
+
* // From Blob or File
|
|
31
|
+
* const blob = new Blob(['data'], { type: 'text/plain' });
|
|
32
|
+
* const bufferFromBlob = await toBuffer(blob);
|
|
34
33
|
* ```
|
|
35
34
|
*/
|
|
36
35
|
export async function toBuffer(input: BinaryInput) {
|
|
37
36
|
if (Buffer.isBuffer(input)) return input;
|
|
38
|
-
return Buffer.from(
|
|
37
|
+
if (input instanceof ArrayBuffer) return Buffer.from(input);
|
|
38
|
+
if (input instanceof Uint8Array) return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
39
|
+
if (typeof input.arrayBuffer === 'function') return Buffer.from(await input.arrayBuffer());
|
|
40
|
+
// eslint-disable-next-line style/max-len
|
|
41
|
+
throw new TypeError('The provided input is not a supported binary type (Blob, Buffer, File, ArrayBuffer, or Uint8Array).');
|
|
39
42
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
export type { FilteredKeyPath } from './filtered-key-path';
|
|
8
8
|
|
|
9
9
|
export type AnyRecord = Record<string, any>;
|
|
10
|
-
export type BinaryInput = Blob | Buffer | File | NodeBlob | NodeFile;
|
|
10
|
+
export type BinaryInput = ArrayBuffer | Blob | Buffer | File | NodeBlob | NodeFile | Uint8Array;
|
|
11
11
|
export type Booleanish = 'false' | 'true' | boolean;
|
|
12
12
|
export type MaybePartial<T> = Partial<T> | T;
|
|
13
13
|
export type MaybeReadonly<T> = Readonly<T> | T;
|