UNPKG

@authup/kit

Version:

A Package containing general (context independent) utilities.

1 lines 16.8 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../src/constants.ts","../src/array.ts","../src/array-buffer.ts","../src/base64.ts","../src/bcrypt.ts","../src/has-own-property.ts","../src/is-simple-match.ts","../src/nanoid.ts","../src/object.ts","../src/pick.ts","../src/scalar.ts","../src/serialize.ts","../src/template.ts","../src/uuid.ts","../src/url.ts","../src/wait.ts"],"sourcesContent":["/*\n * Copyright (c) 2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum DecisionStrategy {\n /**\n * One or more positive\n */\n AFFIRMATIVE = 'affirmative',\n /**\n * All positive\n */\n UNANIMOUS = 'unanimous',\n /**\n * More positive than negative\n */\n CONSENSUS = 'consensus',\n}\n\nexport enum EnvironmentName {\n PRODUCTION = 'production',\n TEST = 'test',\n DEVELOPMENT = 'development',\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function toArray<T>(input?: T | T[]): T[] {\n if (!input) {\n return [];\n }\n\n return Array.isArray(input) ? input : [input];\n}\n\nexport function toStringArray<T>(input?: T | T[]) : string[] {\n return toArray(input)\n .filter((el) => typeof el === 'string') as string[];\n}\n\nexport function toArrayElement<T>(input?: T | T[]): T | undefined {\n return Array.isArray(input) ? input[0] : input;\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function arrayBufferToBase64(buffer: ArrayBuffer) {\n return btoa(String.fromCharCode(...new Uint8Array(buffer)));\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function base64ToArrayBuffer(base64: string) : ArrayBuffer {\n const bin = atob(base64);\n const len = bin.length;\n const bytes = new Uint8Array(len);\n\n for (let i = 0; i < len; i++) {\n bytes[i] = bin.charCodeAt(i);\n }\n\n return bytes.buffer;\n}\n\n/**\n * @see https://thewoods.blog/base64url/\n *\n * @param input\n */\nexport function base64URLEncode(input: string): string {\n return btoa(input)\n .replace(/\\+/g, '-')\n .replace(/\\//g, '_')\n .replace(/=+$/, '');\n}\n\n/**\n * @see https://thewoods.blog/base64url/\n *\n * @param value\n */\nexport function base64URLDecode(value: string): string {\n const m = value.length % 4;\n\n return atob(\n value.replace(/-/g, '+')\n .replace(/_/g, '/')\n .padEnd(value.length + (m === 0 ? 0 : 4 - m), '='),\n );\n}\n","/*\n * Copyright (c) 2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * Check if an input string might be a bcrypt hash.\n *\n * @see https://stackoverflow.com/questions/5393803/can-someone-explain-how-bcrypt-verifies-a-hash\n * @param input\n */\nexport function isBCryptHash(input: string): boolean {\n return [\n '$2a$',\n '$2x$',\n '$2y$',\n '$2b$',\n ].includes(input.substring(0, 4));\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n\nexport function isPropertySet<X extends Record<string, any>, K extends keyof X>(\n obj: X,\n prop: K,\n) : boolean {\n return hasOwnProperty(obj, prop);\n}\n","/*\n * Copyright (c) 2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isSimpleMatch(\n value: string,\n pattern: string | string[],\n) {\n if (Array.isArray(pattern)) {\n for (const element of pattern) {\n if (isSimpleMatch(value, element)) {\n return true;\n }\n }\n\n return false;\n }\n if (value === pattern) {\n return true;\n }\n\n const maxLength = Math.max(value.length, pattern.length);\n\n for (let i = 0; i < maxLength; i++) {\n if (value[i] === pattern[i]) {\n continue;\n }\n\n if (pattern[i] === '*') {\n if (pattern[i + 1] === '*') {\n return true;\n }\n\n let sI : number = -1;\n for (let j = i; j < value.length; j++) {\n if (value[j] === '/') {\n sI = j + 1;\n break;\n }\n }\n\n if (sI === -1) {\n return true;\n }\n\n continue;\n }\n\n return !value[i] &&\n pattern[i] === '/' &&\n pattern[i + 1] === '*';\n }\n\n return false;\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { customAlphabet } from 'nanoid';\n\nexport function createNanoID(alphabet?: string) : string;\nexport function createNanoID(len?: number) : string;\nexport function createNanoID(alphabet?: string, len?: number) : string;\nexport function createNanoID(alphabetOrLen?: string | number, len?: number) : string {\n if (typeof alphabetOrLen === 'string') {\n return customAlphabet(alphabetOrLen, len || 21)();\n }\n\n if (typeof alphabetOrLen === 'number') {\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', alphabetOrLen)();\n }\n\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', len || 21)();\n}\n","/*\n * Copyright (c) 2023-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { ObjectLiteral } from './types';\n\nexport function isObject(input: unknown) : input is Record<string, any> {\n return !!input &&\n typeof input === 'object' &&\n !Array.isArray(input);\n}\n\nexport function extendObject<T extends Record<string, any>>(\n target: T,\n source: Partial<T>,\n) : T {\n const keys = Object.keys(source);\n for (const key of keys) {\n target[key as keyof T] = source[key] as T[keyof T];\n }\n\n return target;\n}\n\nexport function flattenObject(input: Record<string, any>) : Record<string, any> {\n const output : Record<string, any> = {};\n\n const keys = Object.keys(input);\n for (const key of keys) {\n const value = input[key];\n if (isObject(value)) {\n const childAttributes = flattenObject(value);\n const childAttributeKeys = Object.keys(childAttributes);\n for (const childAttributeKey of childAttributeKeys) {\n output[`${key}.${childAttributeKey}`] = childAttributes[childAttributeKey];\n }\n\n continue;\n }\n\n output[key] = input[key];\n }\n\n return output;\n}\n\nexport function removeObjectProperty<T extends ObjectLiteral>(input: Partial<T>, key: keyof T) {\n delete input[key];\n}\n\nexport function omitObjectProperties<\n T extends ObjectLiteral,\n>(input: Record<string, any>, excludeKeys: (keyof T)[]): T {\n const output : T = {} as T;\n const keys = Object.keys(input);\n\n for (const key of keys) {\n if (excludeKeys.includes(key)) {\n continue;\n }\n\n output[key as keyof T] = input[key] as T[keyof T];\n }\n\n return output;\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\ntype PickRecord<T extends Record<string, any>, S extends keyof T> = {\n [K in S]: K extends keyof T ? T[K] : never\n};\n\nexport function pickRecord<\n T extends Record<string, any>,\n K extends keyof T,\n>(data: T, keys: K[]) : PickRecord<T, K> {\n const output : PickRecord<T, K> = {} as PickRecord<T, K>;\n for (const key of keys) {\n output[key] = data[key];\n }\n\n return output;\n}\n\ntype OmitRecord<T extends Record<string, any>, S extends keyof T> = {\n [K in Exclude<keyof T, S>]: T[K]\n};\n\nexport function omitRecord<\n T extends Record<string, any>,\n K extends keyof T,\n>(data: T, keys: K[]) : OmitRecord<T, K> {\n const dataKeys = Object.keys(data) as K[];\n\n let index : number;\n const output = {} as OmitRecord<T, K>;\n for (const dataKey of dataKeys) {\n index = keys.indexOf(dataKey);\n if (index !== -1) {\n continue;\n }\n\n output[dataKey as unknown as keyof OmitRecord<T, K>] = data[dataKey] as unknown as T[Exclude<keyof T, K>];\n }\n\n return output;\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isScalar(\n value: unknown,\n) : value is string | number | boolean | null | undefined {\n return (\n typeof value === 'string' ||\n typeof value === 'number' ||\n typeof value === 'boolean' ||\n value === null ||\n value === undefined\n );\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { destr } from 'destr';\n\nexport function serialize(input: unknown) : string {\n if (typeof input === 'boolean') {\n return input ? 'true' : 'false';\n }\n\n if (typeof input === 'undefined') {\n return 'undefined';\n }\n\n if (input === null) {\n return 'null';\n }\n\n if (typeof input === 'number') {\n return `${input}`;\n }\n\n if (typeof input === 'string') {\n return input;\n }\n\n return JSON.stringify(input, (key, value) => {\n if (value instanceof RegExp) {\n return value.toString();\n }\n\n if (typeof value === 'bigint') {\n return value.toString();\n }\n\n return value;\n });\n}\n\nexport function deserialize<T = any>(input: any) : T {\n return destr(input);\n}\n","/*\n * Copyright (c) 2024-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function template(\n str: string,\n data: Record<string, any>,\n regex = /\\{\\{(.+?)\\}\\}/g,\n) : string {\n return Array.from(str.matchAll(regex))\n .reduce((\n acc,\n match,\n ) => {\n const key = match[1];\n if (\n key &&\n Object.prototype.hasOwnProperty.call(data, key) &&\n typeof data[key] !== 'undefined'\n ) {\n return acc.replace(match[0], data[key]);\n }\n\n return acc;\n }, str);\n}\n","/*\n * Copyright (c) 2023-2026.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nconst regexp = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n\nexport function isUUID(input: string) : boolean {\n return regexp.test(input);\n}\n","/*\n * Copyright (c) 2022-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function makeURLPublicAccessible(url: string) {\n return url.replace('0.0.0.0', '127.0.0.1');\n}\n","/*\n * Copyright (c) 2025.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n/**\n * Wait for x ms.\n *\n * @param ms\n */\nexport async function wait(ms: number): Promise<void> {\n return new Promise<void>((resolve) => {\n setTimeout(resolve, ms);\n });\n}\n"],"mappings":";;;AAOA,IAAY,mBAAL,yBAAA,kBAAA;;;;AAIH,kBAAA,iBAAA;;;;AAIA,kBAAA,eAAA;;;;AAIA,kBAAA,eAAA;;KACH;AAED,IAAY,kBAAL,yBAAA,iBAAA;AACH,iBAAA,gBAAA;AACA,iBAAA,UAAA;AACA,iBAAA,iBAAA;;KACH;;;ACnBD,SAAgB,QAAW,OAAsB;AAC7C,KAAI,CAAC,MACD,QAAO,EAAE;AAGb,QAAO,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM;;AAGjD,SAAgB,cAAiB,OAA4B;AACzD,QAAO,QAAQ,MAAM,CAChB,QAAQ,OAAO,OAAO,OAAO,SAAS;;AAG/C,SAAgB,eAAkB,OAAgC;AAC9D,QAAO,MAAM,QAAQ,MAAM,GAAG,MAAM,KAAK;;;;ACd7C,SAAgB,oBAAoB,QAAqB;AACrD,QAAO,KAAK,OAAO,aAAa,GAAG,IAAI,WAAW,OAAO,CAAC,CAAC;;;;ACD/D,SAAgB,oBAAoB,QAA8B;CAC9D,MAAM,MAAM,KAAK,OAAO;CACxB,MAAM,MAAM,IAAI;CAChB,MAAM,QAAQ,IAAI,WAAW,IAAI;AAEjC,MAAK,IAAI,IAAI,GAAG,IAAI,KAAK,IACrB,OAAM,KAAK,IAAI,WAAW,EAAE;AAGhC,QAAO,MAAM;;;;;;;AAQjB,SAAgB,gBAAgB,OAAuB;AACnD,QAAO,KAAK,MAAM,CACb,QAAQ,OAAO,IAAI,CACnB,QAAQ,OAAO,IAAI,CACnB,QAAQ,OAAO,GAAG;;;;;;;AAQ3B,SAAgB,gBAAgB,OAAuB;CACnD,MA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