@agile-ts/utils
Advanced tools
Comparing version 0.0.9 to 0.0.10
# @agile-ts/utils | ||
## 0.0.10 | ||
### Patch Changes | ||
- e6ef3a7: #### :bug: Bug Fix | ||
- `core`, `multieditor`, `react` | ||
- [#204](https://github.com/agile-ts/agile/pull/204) fix multieditor ([@bennodev19](https://github.com/bennodev19)) | ||
- `api`, `core`, `cra-template-agile-typescript`, `cra-template-agile`, `event`, `logger`, `multieditor`, `proxytree`, `react`, `utils`, `vue` | ||
- [#201](https://github.com/agile-ts/agile/pull/201) fix commonjs bundle ([@bennodev19](https://github.com/bennodev19)) | ||
#### Committers: 1 | ||
- BennoDev ([@bennodev19](https://github.com/bennodev19)) | ||
## 0.0.9 | ||
@@ -4,0 +19,0 @@ |
@@ -5,243 +5,181 @@ 'use strict'; | ||
/** | ||
* Creates a fresh (deep) copy of the specified value. | ||
* https://www.samanthaming.com/tidbits/70-3-ways-to-clone-objects/ | ||
* | ||
* @public | ||
* @param value - Value to be copied. | ||
*/ | ||
function copy(value) { | ||
// Extra checking 'value == null' because 'typeof null === object' | ||
if (value == null || typeof value !== 'object') | ||
return value; | ||
// Ignore everything that is no object or array but has the type of an object (e.g. classes) | ||
const valConstructorName = Object.getPrototypeOf(value).constructor.name.toLowerCase(); | ||
if (valConstructorName !== 'object' && valConstructorName !== 'array') | ||
return value; | ||
let temp; | ||
const newObject = Array.isArray(value) ? [] : {}; | ||
for (const property in value) { | ||
temp = value[property]; | ||
newObject[property] = copy(temp); | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
if (enumerableOnly) { | ||
symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
}); | ||
} | ||
return newObject; | ||
keys.push.apply(keys, symbols); | ||
} | ||
return keys; | ||
} | ||
/** | ||
* Checks whether the specified value is a valid object. | ||
* https://stackoverflow.com/questions/12996871/why-does-typeof-array-with-objects-return-object-and-not-array | ||
* | ||
* @public | ||
* @param value - Value | ||
* @param considerArray - Whether to considered an array as an object. | ||
*/ | ||
function isValidObject(value, considerArray = false) { | ||
function isHTMLElement(obj) { | ||
try { | ||
return obj instanceof HTMLElement; | ||
} | ||
catch (e) { | ||
return (typeof obj === 'object' && | ||
obj.nodeType === 1 && | ||
typeof obj.style === 'object' && | ||
typeof obj.ownerDocument === 'object'); | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i] != null ? arguments[i] : {}; | ||
if (i % 2) { | ||
ownKeys(Object(source), true).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} else if (Object.getOwnPropertyDescriptors) { | ||
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); | ||
} else { | ||
ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
}); | ||
} | ||
return (value !== null && | ||
typeof value === 'object' && | ||
!isHTMLElement(value) && | ||
(considerArray ? true : !Array.isArray(value))); | ||
} | ||
return target; | ||
} | ||
/** | ||
* Checks whether 'array1' contains all elements of 'array2'. | ||
* | ||
* @public | ||
* @param array1 - Array 1 | ||
* @param array2 - Array 2 | ||
*/ | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function copy(value) { | ||
if (value == null || typeof value !== 'object') return value; | ||
var valConstructorName = Object.getPrototypeOf(value).constructor.name.toLowerCase(); | ||
if (valConstructorName !== 'object' && valConstructorName !== 'array') return value; | ||
var temp; | ||
var newObject = Array.isArray(value) ? [] : {}; | ||
for (var property in value) { | ||
temp = value[property]; | ||
newObject[property] = copy(temp); | ||
} | ||
return newObject; | ||
} | ||
function isValidObject(value) { | ||
var considerArray = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
function isHTMLElement(obj) { | ||
try { | ||
return obj instanceof HTMLElement; | ||
} catch (e) { | ||
return typeof obj === 'object' && obj.nodeType === 1 && typeof obj.style === 'object' && typeof obj.ownerDocument === 'object'; | ||
} | ||
} | ||
return value !== null && typeof value === 'object' && !isHTMLElement(value) && (considerArray ? true : !Array.isArray(value)); | ||
} | ||
function includesArray(array1, array2) { | ||
return array2.every((element) => array1.includes(element)); | ||
return array2.every(element => array1.includes(element)); | ||
} | ||
/** | ||
* Transforms Item/s into an array of Items. | ||
* | ||
* @public | ||
* @param items - Item/s to be transformed into an array of Items. | ||
* @param config - Config | ||
*/ | ||
function normalizeArray(items, config = {}) { | ||
config = defineConfig(config, { | ||
createUndefinedArray: false, // If it should return [] or [undefined] if the passed Item is undefined | ||
}); | ||
if (items == null && !config.createUndefinedArray) | ||
return []; | ||
return Array.isArray(items) ? items : [items]; | ||
function normalizeArray(items) { | ||
var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
config = defineConfig(config, { | ||
createUndefinedArray: false | ||
}); | ||
if (items == null && !config.createUndefinedArray) return []; | ||
return Array.isArray(items) ? items : [items]; | ||
} | ||
/** | ||
* Checks whether the specified function is a function. | ||
* | ||
* @public | ||
* @param value - Value to be checked | ||
*/ | ||
function isFunction(value) { | ||
return typeof value === 'function'; | ||
return typeof value === 'function'; | ||
} | ||
/** | ||
* Checks whether the specified function is an async function. | ||
* | ||
* @public | ||
* @param value - Value to be checked. | ||
*/ | ||
function isAsyncFunction(value) { | ||
const valueString = value.toString(); | ||
return (isFunction(value) && | ||
(value.constructor.name === 'AsyncFunction' || | ||
valueString.includes('__awaiter'))); | ||
var valueString = value.toString(); | ||
return isFunction(value) && (value.constructor.name === 'AsyncFunction' || valueString.includes('__awaiter')); | ||
} | ||
/** | ||
* Checks whether the specified value is a valid JSON string | ||
* | ||
* @public | ||
* @param value - Value to be checked. | ||
*/ | ||
function isJsonString(value) { | ||
if (typeof value !== 'string') | ||
return false; | ||
try { | ||
JSON.parse(value); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
return true; | ||
if (typeof value !== 'string') return false; | ||
try { | ||
JSON.parse(value); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Merges the default values object ('defaults') into the configuration object ('config'). | ||
* | ||
* @public | ||
* @param config - Configuration object to merge the default values in. | ||
* @param defaults - Default values object to be merged into the configuration object. | ||
* @param overwriteUndefinedProperties - Whether to overwrite 'undefined' set properties with default values. | ||
*/ | ||
function defineConfig(config, defaults, overwriteUndefinedProperties) { | ||
if (overwriteUndefinedProperties === undefined) | ||
overwriteUndefinedProperties = true; | ||
const shallowCopiedConfig = { ...config }; | ||
for (const defaultKey in defaults) { | ||
if (!Object.prototype.hasOwnProperty.call(shallowCopiedConfig, defaultKey) || | ||
(overwriteUndefinedProperties && | ||
shallowCopiedConfig[defaultKey] === undefined)) | ||
shallowCopiedConfig[defaultKey] = defaults[defaultKey]; | ||
} | ||
return shallowCopiedConfig; | ||
if (overwriteUndefinedProperties === undefined) overwriteUndefinedProperties = true; | ||
var shallowCopiedConfig = _objectSpread2({}, config); | ||
for (var defaultKey in defaults) { | ||
if (!Object.prototype.hasOwnProperty.call(shallowCopiedConfig, defaultKey) || overwriteUndefinedProperties && shallowCopiedConfig[defaultKey] === undefined) shallowCopiedConfig[defaultKey] = defaults[defaultKey]; | ||
} | ||
return shallowCopiedConfig; | ||
} | ||
/** | ||
* Merges the 'changes' object into the 'source' object at top level. | ||
* | ||
* @public | ||
* @param source - Source object | ||
* @param changes - Changes object to be merged into the source object | ||
* @param config - Configuration object | ||
*/ | ||
function flatMerge(source, changes, config = {}) { | ||
config = defineConfig(config, { | ||
addNewProperties: true, | ||
}); | ||
// Copy Source to avoid References | ||
const _source = copy(source); | ||
if (_source == null) | ||
return _source; | ||
// Merge Changes Object into Source Object | ||
const keys = Object.keys(changes); | ||
keys.forEach((property) => { | ||
if ((!config.addNewProperties && _source[property] != null) || | ||
config.addNewProperties) | ||
_source[property] = changes[property]; | ||
}); | ||
return _source; | ||
function flatMerge(source, changes) { | ||
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
config = defineConfig(config, { | ||
addNewProperties: true | ||
}); | ||
var _source = copy(source); | ||
if (_source == null) return _source; | ||
var keys = Object.keys(changes); | ||
keys.forEach(property => { | ||
if (!config.addNewProperties && _source[property] != null || config.addNewProperties) _source[property] = changes[property]; | ||
}); | ||
return _source; | ||
} | ||
/** | ||
* Checks whether the two specified values are equivalent. | ||
* | ||
* @public | ||
* @param value1 - First value. | ||
* @param value2 - Second value. | ||
*/ | ||
function equal(value1, value2) { | ||
return (value1 === value2 || | ||
// Checking if 'value1' and 'value2' is typeof object before | ||
// using the JSON.stringify comparison to optimize the performance | ||
(typeof value1 === 'object' && | ||
typeof value2 === 'object' && | ||
JSON.stringify(value1) === JSON.stringify(value2))); | ||
return value1 === value2 || typeof value1 === 'object' && typeof value2 === 'object' && JSON.stringify(value1) === JSON.stringify(value2); | ||
} | ||
/** | ||
* Checks whether the two specified values are NOT equivalent. | ||
* | ||
* @public | ||
* @param value1 - First value. | ||
* @param value2 - Second value. | ||
*/ | ||
function notEqual(value1, value2) { | ||
return !equal(value1, value2); | ||
return !equal(value1, value2); | ||
} | ||
/** | ||
* Generates a randomized id based on alphabetic and numeric characters. | ||
* | ||
* @public | ||
* @param length - Length of the to generate id (default = 5). | ||
* @param characters - Characters to generate the id from (default = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'). | ||
*/ | ||
function generateId(length = 5, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') { | ||
const charactersLength = characters.length; | ||
let result = ''; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
return result; | ||
function generateId() { | ||
var length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5; | ||
var characters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
var charactersLength = characters.length; | ||
var result = ''; | ||
for (var i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Transforms the specified object into an array. | ||
* | ||
* Example: | ||
* {"1": 'jeff', 2: 'frank'} -> [{key: "1", instance: 'jeff'}, {key: 2, instance: 'frank'}] | ||
* | ||
* @public | ||
* @param object - Object to be transformed to an array. | ||
*/ | ||
function createArrayFromObject(object) { | ||
const array = []; | ||
for (const key in object) { | ||
array.push({ | ||
key: key, | ||
instance: object[key], | ||
}); | ||
} | ||
return array; | ||
var array = []; | ||
for (var key in object) { | ||
array.push({ | ||
key: key, | ||
instance: object[key] | ||
}); | ||
} | ||
return array; | ||
} | ||
/** | ||
* Clones the specified class. | ||
* | ||
* @public | ||
* @param instance - Class to be cloned. | ||
*/ | ||
function clone(instance) { | ||
// Clone Class | ||
const objectCopy = Object.create(Object.getPrototypeOf(instance)); | ||
const objectClone = Object.assign(objectCopy, instance); | ||
// Copy Properties of Class to remove flat references | ||
for (const key in objectClone) | ||
objectClone[key] = copy(objectClone[key]); | ||
return objectClone; | ||
var objectCopy = Object.create(Object.getPrototypeOf(instance)); | ||
var objectClone = Object.assign(objectCopy, instance); | ||
for (var key in objectClone) { | ||
objectClone[key] = copy(objectClone[key]); | ||
} | ||
return objectClone; | ||
} | ||
/** | ||
* Removes specified properties from the defined object. | ||
* | ||
* @public | ||
* @param object - Object to remove the specified properties from. | ||
* @param properties - Property keys to be removed from the specified object. | ||
*/ | ||
function removeProperties(object, properties) { | ||
const copiedObject = copy(object); | ||
properties.map((property) => delete copiedObject[property]); | ||
return copiedObject; | ||
var copiedObject = copy(object); | ||
properties.map(property => delete copiedObject[property]); | ||
return copiedObject; | ||
} | ||
@@ -264,1 +202,2 @@ | ||
exports.removeProperties = removeProperties; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@agile-ts/utils", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"author": "BennoDev", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39028
439