UNPKG

@amaui/utils

Version:
876 lines (611 loc) 28.9 kB
import is from './is'; import isEnvironment from './isEnvironment'; import alpha from './alpha'; import equalDeep from './equalDeep'; import order from './order'; import encrypt from './encrypt'; import decrypt from './decrypt'; import encode from './encode'; import lighten from './lighten'; import darken from './darken'; import emphasize from './emphasize'; import getLuminance from './getLuminance'; import getContrastRatio from './getContrastRatio'; import decode from './decode'; import serialize from './serialize'; import deserialize from './deserialize'; import colorToRgb from './colorToRgb'; import rgbToHex from './rgbToHex'; import rgbToHsl from './rgbToHsl'; import rgbToRgba from './rgbToRgba'; import hslToRgb from './hslToRgb'; import hexToRgb from './hexToRgb'; import stringify from './stringify'; import binaryStringToHexadecimal from './binaryStringToHexadecimal'; import hexadecimalStringToBinary from './hexadecimalStringToBinary'; import parse from './parse'; import merge from './merge'; import hash from './hash'; import hashFile from './hashFile'; import getURL from './getURL'; import getOrdinalNumber from './getOrdinalNumber'; import getStringVariables from './getStringVariables'; import setStringVariables from './setStringVariables'; import flattenObject from './flattenObject'; import unflattenObject from './unflattenObject'; import hasObjectProperty from './hasObjectProperty'; import hasObjectPropertyValue from './hasObjectPropertyValue'; import getObjectValue from './getObjectValue'; import getObjectPropertyValue from './getObjectPropertyValue'; import setObjectValue from './setObjectValue'; import removeObjectValue from './removeObjectValue'; import unique from './unique'; import simpleNormalize from './simpleNormalize'; import getLeadingZerosNumber from './getLeadingZerosNumber'; import getFileName from './getFileName'; import fileToValue from './fileToValue'; import copy from './copy'; import clamp from './clamp'; import copyToClipboard from './copyToClipboard'; import capitalize from './capitalize'; import castParam from './castParam'; import cleanValue from './cleanValue'; import to from './to'; import pick from './pick'; import arrayToParts from './arrayToParts'; import slugify from './slugify'; import shuffle from './shuffle'; import updateQueryParams from './updateQueryParams'; import checkObjectFilters from './checkObjectFilters'; import numberWithCommas from './numberWithCommas'; import arrayMoveItem from './arrayMoveItem'; import factorial from './factorial'; import permutation from './permutation'; import permutationWithRepetition from './permutationWithRepetition'; import variation from './variation'; import variationWithRepetition from './variationWithRepetition'; import combination from './combination'; import combinationWithRepetition from './combinationWithRepetition'; import promisify from './promisify'; export const stringPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line String.prototype.alpha = function (opacity) { return alpha(this, opacity); }; // eslint-disable-next-line String.prototype.encode = function () { return encode(this); }; // eslint-disable-next-line String.prototype.encrypt = function (privateValue) { return encrypt(this, privateValue); }; // eslint-disable-next-line String.prototype.simpleNormalize = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return simpleNormalize(this, options); }; // eslint-disable-next-line String.prototype.getVariables = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getStringVariables(this, options); }; // eslint-disable-next-line String.prototype.setVariables = function () { let variablesToValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return setStringVariables(this, variablesToValue, options); }; // eslint-disable-next-line String.prototype.getOrdinalNumber = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getOrdinalNumber(this, options); }; // eslint-disable-next-line String.prototype.getURL = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getURL(this, options); }; // eslint-disable-next-line String.prototype.stringify = function (spaces) { return stringify(this, spaces); }; // eslint-disable-next-line String.prototype.parse = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'JSON'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return parse(this, type, options); }; // eslint-disable-next-line String.prototype.colorToRgb = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return colorToRgb(this, opacity, array); }; // eslint-disable-next-line String.prototype.rgbToHex = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return rgbToHex(this, opacity, array); }; // eslint-disable-next-line String.prototype.rgbToHsl = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return rgbToHsl(this, opacity, array); }; // eslint-disable-next-line String.prototype.rgbToRgba = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return rgbToRgba(this, opacity, array); }; // eslint-disable-next-line String.prototype.hslToRgb = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return hslToRgb(this, opacity, array); }; // eslint-disable-next-line String.prototype.hexToRgb = function () { let opacity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; let array = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return hexToRgb(this, opacity, array); }; // eslint-disable-next-line String.prototype.serialize = function () { return serialize(this); }; // eslint-disable-next-line String.prototype.decode = function () { return decode(this); }; // eslint-disable-next-line String.prototype.deserialize = function () { return deserialize(this); }; // eslint-disable-next-line String.prototype.decrypt = function (privateValue) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return decrypt(this, privateValue); }; // eslint-disable-next-line String.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line String.prototype.lighten = function (coefficient) { return lighten(this, coefficient); }; // eslint-disable-next-line String.prototype.darken = function (coefficient) { return darken(this, coefficient); }; // eslint-disable-next-line String.prototype.emphasize = function () { let coefficient = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0.14; return emphasize(this, coefficient); }; // eslint-disable-next-line String.prototype.binaryStringToHexadecimal = function () { return binaryStringToHexadecimal(this); }; // eslint-disable-next-line String.prototype.hexadecimalStringToBinary = function () { return hexadecimalStringToBinary(this); }; // eslint-disable-next-line String.prototype.getLeadingZerosNumber = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getLeadingZerosNumber(this, options); }; // eslint-disable-next-line String.prototype.getLuminance = function () { return getLuminance(this); }; // eslint-disable-next-line String.prototype.getContrastRatio = function (valueB) { return getContrastRatio(this, valueB); }; // eslint-disable-next-line String.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hash(this, options); }; // eslint-disable-next-line String.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; // eslint-disable-next-line String.prototype.copyToClipboard = function () { return copyToClipboard(this); }; // eslint-disable-next-line String.prototype.numberWithCommas = function () { let delimiter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ','; return numberWithCommas(this, delimiter); }; // eslint-disable-next-line String.prototype.capitalize = function () { return capitalize(this); }; // eslint-disable-next-line String.prototype.castParam = function (options) { return castParam(this, options); }; // eslint-disable-next-line String.prototype.clean = function (options) { return cleanValue(this, options); }; // eslint-disable-next-line String.prototype.pick = function () { let min = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1; let max = arguments.length > 1 ? arguments[1] : undefined; return pick(this, min, max); }; // eslint-disable-next-line String.prototype.slugify = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return slugify(this, options); }; // eslint-disable-next-line String.prototype.shuffle = function () { let toShuffle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 14; return shuffle(this, toShuffle); }; // eslint-disable-next-line String.prototype.to = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'arraybuffer'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return to(this, type, options); }; } }; export const numberPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line Number.prototype.encode = function () { return encode(this); }; // eslint-disable-next-line Number.prototype.factorial = function () { return factorial(this); }; // eslint-disable-next-line Number.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hash(this, options); }; // eslint-disable-next-line Number.prototype.encrypt = function (privateValue) { return encrypt(this, privateValue); }; // eslint-disable-next-line Number.prototype.stringify = function (spaces) { return stringify(this, spaces); }; // eslint-disable-next-line Number.prototype.simpleNormalize = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return simpleNormalize(this, options); }; // eslint-disable-next-line Number.prototype.parse = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'JSON'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return parse(this, type, options); }; // eslint-disable-next-line Number.prototype.serialize = function () { return serialize(this); }; // eslint-disable-next-line Number.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line Number.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; // eslint-disable-next-line Number.prototype.getLeadingZeros = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getLeadingZerosNumber(this, options); }; // eslint-disable-next-line Number.prototype.getOrdinal = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return getOrdinalNumber(this, options); }; // eslint-disable-next-line Number.prototype.copyToClipboard = function () { return copyToClipboard(this); }; // eslint-disable-next-line Number.prototype.clamp = function (min, max) { return clamp(this, min, max); }; // eslint-disable-next-line Number.prototype.castParam = function (options) { return castParam(this, options); }; // eslint-disable-next-line Number.prototype.withCommas = function () { let delimiter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ','; return numberWithCommas(this, delimiter); }; // eslint-disable-next-line Number.prototype.to = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'arraybuffer'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return to(this, type, options); }; } }; export const booleanPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line Boolean.prototype.encode = function () { return encode(this); }; // eslint-disable-next-line Boolean.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hash(this, options); }; // eslint-disable-next-line Boolean.prototype.encrypt = function (privateValue) { return encrypt(this, privateValue); }; // eslint-disable-next-line Boolean.prototype.stringify = function (spaces) { return stringify(this, spaces); }; // eslint-disable-next-line Boolean.prototype.simpleNormalize = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return simpleNormalize(this, options); }; // eslint-disable-next-line Boolean.prototype.parse = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'JSON'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return parse(this, type, options); }; // eslint-disable-next-line Boolean.prototype.serialize = function () { return serialize(this); }; // eslint-disable-next-line Boolean.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line Boolean.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; // eslint-disable-next-line Boolean.prototype.copyToClipboard = function () { return copyToClipboard(this); }; // eslint-disable-next-line Boolean.prototype.castParam = function (options) { return castParam(this, options); }; // eslint-disable-next-line Boolean.prototype.to = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'arraybuffer'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return to(this, type, options); }; } }; export const arrayPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line Array.prototype.order = function () { return order(this); }; // eslint-disable-next-line Array.prototype.permutation = function (options) { return permutation(this, options); }; // eslint-disable-next-line Array.prototype.permutationWithRepetition = function (options) { return permutationWithRepetition(this, options); }; // eslint-disable-next-line Array.prototype.variation = function (items, options) { return variation(this, items, options); }; // eslint-disable-next-line Array.prototype.variationWithRepetition = function (items, options) { return variationWithRepetition(this, items, options); }; // eslint-disable-next-line Array.prototype.combination = function (items, options) { return combination(this, items, options); }; // eslint-disable-next-line Array.prototype.combinationWithRepetition = function (items, options) { return combinationWithRepetition(this, items, options); }; // eslint-disable-next-line Array.prototype.unique = function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return unique(this, ...args); }; // eslint-disable-next-line Array.prototype.encode = function () { return encode(this); }; // eslint-disable-next-line Array.prototype.stringify = function (spaces) { return stringify(this, spaces); }; // eslint-disable-next-line Array.prototype.getValue = function () { for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { args[_key2] = arguments[_key2]; } return getObjectValue(this, ...args); }; // eslint-disable-next-line Array.prototype.setValue = function (keys) { let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; let options = arguments.length > 2 ? arguments[2] : undefined; return setObjectValue(this, keys, value, options); }; // eslint-disable-next-line Array.prototype.removeValue = function (keys) { return removeObjectValue(this, keys); }; // eslint-disable-next-line Array.prototype.getPropertyValue = function (keys) { return getObjectPropertyValue(this, keys); }; // eslint-disable-next-line Array.prototype.hasProperty = function () { for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { args[_key3] = arguments[_key3]; } return hasObjectProperty(this, ...args); }; // eslint-disable-next-line Array.prototype.hasPropertyValue = function (keys) { return hasObjectPropertyValue(this, keys); }; // eslint-disable-next-line Array.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hash(this, options); }; // eslint-disable-next-line Array.prototype.encrypt = function (privateValue) { return encrypt(this, privateValue); }; // eslint-disable-next-line Array.prototype.merge = function (source) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return merge(this, source, options); }; // eslint-disable-next-line Array.prototype.parse = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'JSON'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return parse(this, type, options); }; // eslint-disable-next-line Array.prototype.serialize = function () { return serialize(this); }; // eslint-disable-next-line Array.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line Array.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; // eslint-disable-next-line Array.prototype.copyToClipboard = function () { return copyToClipboard(this); }; // eslint-disable-next-line Array.prototype.copy = function () { return copy(this); }; // eslint-disable-next-line Array.prototype.moveItem = function (oldIndex, newIndex) { return arrayMoveItem(this, oldIndex, newIndex); }; // eslint-disable-next-line Array.prototype.toParts = function (parts) { return arrayToParts(this, parts); }; } }; export const objectPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line Object.prototype.order = function () { return order(this); }; // eslint-disable-next-line Object.prototype.encode = function () { return encode(this); }; // eslint-disable-next-line Object.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hash(this, options); }; // eslint-disable-next-line Object.prototype.encrypt = function (privateValue) { return encrypt(this, privateValue); }; // eslint-disable-next-line Object.prototype.stringify = function (spaces) { return stringify(this, spaces); }; // eslint-disable-next-line Object.prototype.getValue = function () { for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { args[_key4] = arguments[_key4]; } return getObjectValue(this, ...args); }; // eslint-disable-next-line Object.prototype.setValue = function (keys) { let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; let options = arguments.length > 2 ? arguments[2] : undefined; return setObjectValue(this, keys, value, options); }; // eslint-disable-next-line Object.prototype.removeValue = function (keys) { return removeObjectValue(this, keys); }; // eslint-disable-next-line Object.prototype.hasProperty = function () { for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { args[_key5] = arguments[_key5]; } return hasObjectProperty(this, ...args); }; // eslint-disable-next-line Object.prototype.hasPropertyValue = function (keys) { return hasObjectPropertyValue(this, keys); }; // eslint-disable-next-line Object.prototype.getPropertyValue = function (keys) { return getObjectPropertyValue(this, keys); }; // eslint-disable-next-line Object.prototype.flatten = function () { return flattenObject(this); }; // eslint-disable-next-line Object.prototype.unflatten = function () { return unflattenObject(this); }; // eslint-disable-next-line Object.prototype.merge = function (source) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return merge(this, source, options); }; // eslint-disable-next-line Object.prototype.parse = function () { let type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'JSON'; let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return parse(this, type, options); }; // eslint-disable-next-line Object.prototype.serialize = function () { return serialize(this); }; // eslint-disable-next-line Object.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line Object.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; // eslint-disable-next-line Object.prototype.copyToClipboard = function () { return copyToClipboard(this); }; // eslint-disable-next-line Object.prototype.copy = function () { return copy(this); }; // eslint-disable-next-line Object.prototype.updateQueryParams = function () { let override = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; return updateQueryParams(this, override); }; // eslint-disable-next-line Object.prototype.checkFilters = function (filters, operator) { return checkObjectFilters(this, filters, operator); }; } }; export const filePolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (isEnvironment('browser') && additions) { // eslint-disable-next-line File.prototype.hash = function () { let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; return hashFile(this, options); }; // eslint-disable-next-line File.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line File.prototype.getName = function (options) { return getFileName(this, options); }; // eslint-disable-next-line File.prototype.toValue = function (type) { return fileToValue(this, type); }; } }; export const blobPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (isEnvironment('browser') && additions) { // eslint-disable-next-line Blob.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; } }; export const functionPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (additions) { // eslint-disable-next-line Function.prototype.promisify = function () { return promisify(this); }; // eslint-disable-next-line Function.prototype.equalDeep = function (valueB) { return equalDeep(this, valueB); }; // eslint-disable-next-line Function.prototype.is = function (type) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : undefined; return is(type, this, options); }; } }; function arrayBufferFunction() { // this: File or Blob return new Promise(resolve => { const fileReader = new FileReader(); fileReader.onload = () => resolve(fileReader.result); fileReader.readAsArrayBuffer(this); }); } export const arrayBufferPolyfill = () => { var _AMAUI; const AMAUI_TEST = ((_AMAUI = window.AMAUI) === null || _AMAUI === void 0 ? void 0 : _AMAUI.env) === 'test'; if ('File' in window) File.prototype.arrayBuffer = AMAUI_TEST ? arrayBufferFunction || File.prototype.arrayBuffer : File.prototype.arrayBuffer || arrayBufferFunction; if ('Blob' in window) Blob.prototype.arrayBuffer = AMAUI_TEST ? arrayBufferFunction || Blob.prototype.arrayBuffer : Blob.prototype.arrayBuffer || arrayBufferFunction; }; export const browserPolyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; if (isEnvironment('browser')) { // ArrayBuffer arrayBufferPolyfill(); } }; const polyfills = function () { let additions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; // String stringPolyfills(additions); // // Number numberPolyfills(additions); // Boolean booleanPolyfills(additions); // Array arrayPolyfills(additions); // Object objectPolyfills(additions); // File filePolyfills(additions); // Blob blobPolyfills(additions); // Function functionPolyfills(additions); // Browser browserPolyfills(additions); }; export default polyfills;