UNPKG

@authup/kit

Version:

A Package containing general (context independent) utilities.

222 lines (207 loc) 6.72 kB
'use strict'; var nanoid = require('nanoid'); var destr = require('destr'); /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function toArray(input) { if (!input) { return []; } return Array.isArray(input) ? input : [ input ]; } function toStringArray(input) { return toArray(input).filter((el)=>typeof el === 'string'); } function toArrayElement(input) { return Array.isArray(input) ? input[0] : input; } /* * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function arrayBufferToBase64(buffer) { return btoa(String.fromCharCode(...new Uint8Array(buffer))); } /* * Copyright (c) 2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function base64ToArrayBuffer(base64) { const bin = atob(base64); const len = bin.length; const bytes = new Uint8Array(len); for(let i = 0; i < len; i++){ bytes[i] = bin.charCodeAt(i); } return bytes.buffer; } /* * Copyright (c) 2021-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ // eslint-disable-next-line @typescript-eslint/ban-types function hasOwnProperty(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } function isPropertySet(obj, prop) { return hasOwnProperty(obj, prop); } function createNanoID(alphabetOrLen, len) { if (typeof alphabetOrLen === 'string') { return nanoid.customAlphabet(alphabetOrLen, len || 21)(); } if (typeof alphabetOrLen === 'number') { return nanoid.customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', alphabetOrLen)(); } return nanoid.customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', len || 21)(); } /* * Copyright (c) 2023-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isObject(input) { return !!input && typeof input === 'object' && !Array.isArray(input); } function extendObject(target, source) { const keys = Object.keys(source); for(let i = 0; i < keys.length; i++){ target[keys[i]] = source[keys[i]]; } return target; } function flattenObject(input) { const output = {}; const keys = Object.keys(input); for(let i = 0; i < keys.length; i++){ const key = keys[i]; const value = input[key]; if (isObject(value)) { const childAttributes = flattenObject(value); const childAttributeKeys = Object.keys(childAttributes); for(let j = 0; j < childAttributeKeys.length; j++){ output[`${key}.${childAttributeKeys[j]}`] = childAttributes[childAttributeKeys[j]]; } continue; } output[key] = input[key]; } return output; } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function pickRecord(data, keys) { const output = {}; for(let i = 0; i < keys.length; i++){ output[keys[i]] = data[keys[i]]; } return output; } function omitRecord(data, keys) { const dataKeys = Object.keys(data); let index; const output = {}; for(let i = 0; i < dataKeys.length; i++){ index = keys.indexOf(dataKeys[i]); if (index !== -1) { continue; } output[dataKeys[i]] = data[dataKeys[i]]; } return output; } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function isScalar(value) { return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null || value === undefined; } function serialize(input) { if (typeof input === 'boolean') { return input ? 'true' : 'false'; } if (typeof input === 'undefined') { return 'undefined'; } if (input === null) { return 'null'; } if (typeof input === 'number') { return `${input}`; } if (typeof input === 'string') { return input; } return JSON.stringify(input, (key, value)=>{ if (value instanceof RegExp) { return value.toString(); } return value; }); } function deserialize(input) { return destr.destr(input); } /* * Copyright (c) 2024-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function template(str, data, regex = /\{\{(.+?)\}\}/g) { return Array.from(str.matchAll(regex)).reduce((acc, match)=>{ if (typeof data[match[1]] !== 'undefined') { return acc.replace(match[0], data[match[1]]); } return acc; }, str); } /* * Copyright (c) 2023-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ const 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; function isUUID(input) { return regexp.test(input); } /* * Copyright (c) 2022-2024. * Author Peter Placzek (tada5hi) * For the full copyright and license information, * view the LICENSE file that was distributed with this source code. */ function makeURLPublicAccessible(url) { return url.replace('0.0.0.0', '127.0.0.1'); } exports.arrayBufferToBase64 = arrayBufferToBase64; exports.base64ToArrayBuffer = base64ToArrayBuffer; exports.createNanoID = createNanoID; exports.deserialize = deserialize; exports.extendObject = extendObject; exports.flattenObject = flattenObject; exports.hasOwnProperty = hasOwnProperty; exports.isObject = isObject; exports.isPropertySet = isPropertySet; exports.isScalar = isScalar; exports.isUUID = isUUID; exports.makeURLPublicAccessible = makeURLPublicAccessible; exports.omitRecord = omitRecord; exports.pickRecord = pickRecord; exports.serialize = serialize; exports.template = template; exports.toArray = toArray; exports.toArrayElement = toArrayElement; exports.toStringArray = toStringArray; //# sourceMappingURL=index.cjs.map