@ukon1990/js-utilities
Advanced tools
Comparing version 1.6.2 to 1.6.3
export declare class ArrayUtil { | ||
/** | ||
* Checks if a value is an object and not array | ||
*/ | ||
* Checks if a value is an object and not array | ||
*/ | ||
static isArray(value: any): boolean; | ||
/** | ||
* Returns true if an array is populated | ||
*/ | ||
* Returns true if an array is populated | ||
*/ | ||
static isPopulatedArray(value: any): boolean; | ||
@@ -27,3 +27,3 @@ /** | ||
*/ | ||
static removeDuplicates(fromArray: any[]): void; | ||
static removeDuplicates(fromArray: any[], immutable?: boolean): void | any[]; | ||
/** | ||
@@ -35,3 +35,3 @@ * Removes all entries that is identical to a value from an array, | ||
*/ | ||
static removeObject(value: any, fromArray: any[]): void; | ||
static removeObject(value: any, fromArray: any[], immutable?: boolean): void | any[]; | ||
/** | ||
@@ -38,0 +38,0 @@ * Removes multiple objects/values from an array. |
@@ -7,12 +7,13 @@ import { EmptyUtil } from './empty.util'; | ||
/** | ||
* Checks if a value is an object and not array | ||
*/ | ||
* Checks if a value is an object and not array | ||
*/ | ||
static isArray(value) { | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
return (!EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
value.forEach && value.push; | ||
value.forEach && | ||
value.push); | ||
} | ||
/** | ||
* Returns true if an array is populated | ||
*/ | ||
* Returns true if an array is populated | ||
*/ | ||
static isPopulatedArray(value) { | ||
@@ -57,3 +58,4 @@ return ArrayUtil.isArray(value) && Object.keys(value).length > 0; | ||
const differences = new Array(); | ||
if (EmptyUtil.isNullOrUndefined(array1) || EmptyUtil.isNullOrUndefined(array2)) { | ||
if (EmptyUtil.isNullOrUndefined(array1) || | ||
EmptyUtil.isNullOrUndefined(array2)) { | ||
differences.push(new Difference('array', array1, array2)); | ||
@@ -70,15 +72,22 @@ } | ||
*/ | ||
static removeDuplicates(fromArray) { | ||
static removeDuplicates(fromArray, immutable = false) { | ||
const map = {}; | ||
const removeIndexes = []; | ||
const newArray = []; | ||
fromArray.forEach((value, index) => { | ||
const id = JSON.stringify(value); | ||
if (map[id]) { | ||
if (!map[id]) { | ||
map[id] = true; | ||
newArray.push(value); | ||
} | ||
else if (!immutable) { | ||
removeIndexes.push(index); | ||
} | ||
else { | ||
map[id] = true; | ||
} | ||
}); | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
if (!immutable) { | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
} | ||
else { | ||
return newArray; | ||
} | ||
} | ||
@@ -91,4 +100,5 @@ /** | ||
*/ | ||
static removeObject(value, fromArray) { | ||
static removeObject(value, fromArray, immutable = false) { | ||
const removeIndexes = []; | ||
const newArray = []; | ||
fromArray.forEach((v, index) => { | ||
@@ -105,7 +115,15 @@ let equal = false; | ||
} | ||
if (equal) { | ||
if (equal && !immutable) { | ||
removeIndexes.push(index); | ||
} | ||
else if (immutable) { | ||
newArray.push(v); | ||
} | ||
}); | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
if (!immutable) { | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
} | ||
else { | ||
return newArray; | ||
} | ||
} | ||
@@ -124,4 +142,3 @@ /** | ||
static removeIndexes(indexes, fromArray) { | ||
indexes.sort((a, b) => b - a) | ||
.forEach(i => fromArray.splice(i, 1)); | ||
indexes.sort((a, b) => b - a).forEach(i => fromArray.splice(i, 1)); | ||
} | ||
@@ -128,0 +145,0 @@ /** |
@@ -80,3 +80,3 @@ import { ObjectUtil } from './object.util'; | ||
expect(ObjectUtil.isEqual(obj1, obj2)).toBeFalsy(); | ||
expect(ObjectUtil.getDifference(document, document).length).toBeFalsy(); | ||
expect(ObjectUtil.getDifference(obj2, obj2).length).toBeFalsy(); | ||
}); | ||
@@ -83,0 +83,0 @@ }); |
{ | ||
"name": "@ukon1990/js-utilities", | ||
"version": "1.6.2", | ||
"version": "1.6.3", | ||
"description": "A light weight package for object and array manipulation. As well as some utilities for matching text.", | ||
@@ -9,5 +9,5 @@ "main": "dist/index.js", | ||
"build": "tsc", | ||
"test": "ng test --code-coverage", | ||
"test": "jest --forceExit --coverage --verbose", | ||
"headless-test": "ng test --watch=false --browsers ChromeHeadless --code-coverage", | ||
"deploy": "npm run headless-test && tsc && npm publish --access public" | ||
"deploy": "npm run test && tsc && npm publish --access public" | ||
}, | ||
@@ -32,32 +32,12 @@ "keywords": [ | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@angular-devkit/build-angular": "~0.802.2", | ||
"@angular/cli": "~8.2.2", | ||
"@angular/compiler": "~8.2.2", | ||
"@angular/compiler-cli": "~8.2.2", | ||
"@angular/core": "~8.2.2", | ||
"@angular/language-service": "~8.2.2", | ||
"@types/jasmine": "~3.4.0", | ||
"@types/jasminewd2": "~2.0.6", | ||
"@types/node": "~12.7.2", | ||
"codelyzer": "^5.1.0", | ||
"jasmine-core": "~3.4.0", | ||
"jasmine-spec-reporter": "~4.2.1", | ||
"karma": "^4.2.0", | ||
"karma-chrome-launcher": "~3.1.0", | ||
"karma-coverage": "^1.1.2", | ||
"karma-coverage-istanbul-reporter": "~2.1.0", | ||
"karma-jasmine": "~2.0.1", | ||
"karma-jasmine-html-reporter": "^1.4.2", | ||
"karma-typescript": "^4.1.1", | ||
"protractor": "~5.4.2", | ||
"rxjs": "~6.5.2", | ||
"ts-loader": "^6.0.4", | ||
"ts-node": "~8.3.0", | ||
"tslint": "~5.18.0", | ||
"typescript": "3.5.3", | ||
"webpack": "^4.39.2", | ||
"zone.js": "^0.10.2" | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.8", | ||
"jest": "^24.9.0", | ||
"ts-jest": "^24.1.0", | ||
"ts-loader": "^6.2.0", | ||
"ts-node": "^8.4.1", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.3" | ||
} | ||
} |
@@ -22,7 +22,7 @@ # Javascript utilities | ||
* `getDifference(array1, array2)` - Returns an array of differences as `Difference` objects. | ||
* `removeObject(value, fromArray)` - Removes all entries that is identical to a value from an array, regardless of the object reference. | ||
* `removeObject(value, fromArray, immutable = false)` - Removes all entries that is identical to a value from an array, regardless of the object reference. If , immutable = true, it will return a copy instead of mutating the input array. | ||
* `removeObjects(values, fromArray)` - Removes multiple objects/values from an array. | ||
* `removeIndexes(indexes, fromArray)` - Removes the elements from an array at the supplied index locations | ||
* `removeDuplicates(fromArray)` - Remove all duplicate entries in an array. | ||
* `randomOrder(fromArray)` - Returns an array with the items in the fromArray in random order. | ||
* `removeDuplicates(fromArray, immutable = false)` - Remove all duplicate entries in an array. If , immutable = true, it will return a copy instead of mutating the input array. | ||
* `randomOrder(fromArray)` - Returns a new array with the items in the fromArray in random order. | ||
@@ -29,0 +29,0 @@ ## TextUtil |
@@ -1,154 +0,181 @@ | ||
import {EmptyUtil} from './empty.util'; | ||
import {ObjectUtil} from './object.util'; | ||
import {Difference} from '../models/difference.model'; | ||
import {CompareUtil} from './compare.util'; | ||
import { EmptyUtil } from './empty.util'; | ||
import { ObjectUtil } from './object.util'; | ||
import { Difference } from '../models/difference.model'; | ||
import { CompareUtil } from './compare.util'; | ||
export class ArrayUtil { | ||
/** | ||
* Checks if a value is an object and not array | ||
*/ | ||
public static isArray(value: any): boolean { | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
value.forEach && value.push; | ||
} | ||
/** | ||
* Checks if a value is an object and not array | ||
*/ | ||
public static isArray(value: any): boolean { | ||
return ( | ||
!EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
value.forEach && | ||
value.push | ||
); | ||
} | ||
/** | ||
* Returns true if an array is populated | ||
*/ | ||
public static isPopulatedArray(value: any): boolean { | ||
return ArrayUtil.isArray(value) && Object.keys(value).length > 0; | ||
/** | ||
* Returns true if an array is populated | ||
*/ | ||
public static isPopulatedArray(value: any): boolean { | ||
return ArrayUtil.isArray(value) && Object.keys(value).length > 0; | ||
} | ||
/** | ||
* Returns a cloned version of an array. Removing all child object references. | ||
*/ | ||
public static clone(array: Array<any>): Array<any> { | ||
if (EmptyUtil.isNullOrUndefined(array)) { | ||
console.error('Could not clone an array because it is null or undefined'); | ||
return array; | ||
} | ||
/** | ||
* Returns a cloned version of an array. Removing all child object references. | ||
*/ | ||
public static clone(array: Array<any>): Array<any> { | ||
if (EmptyUtil.isNullOrUndefined(array)) { | ||
console.error('Could not clone an array because it is null or undefined'); | ||
return array; | ||
} | ||
const newArray = Array<any>(); | ||
array.forEach(value => { | ||
ArrayUtil.cloneField(value, newArray); | ||
}); | ||
return newArray; | ||
} | ||
const newArray = Array<any>(); | ||
array.forEach(value => { | ||
ArrayUtil.cloneField(value, newArray); | ||
}); | ||
return newArray; | ||
private static cloneField(value: any, newArray: any[]) { | ||
if (ArrayUtil.isArray(value)) { | ||
newArray.push(ArrayUtil.clone(value)); | ||
} else if (ObjectUtil.isObject(value)) { | ||
newArray.push(ObjectUtil.clone(value)); | ||
} else { | ||
newArray.push(value); | ||
} | ||
} | ||
private static cloneField(value: any, newArray: any[]) { | ||
if (ArrayUtil.isArray(value)) { | ||
newArray.push(ArrayUtil.clone(value)); | ||
} else if (ObjectUtil.isObject(value)) { | ||
newArray.push(ObjectUtil.clone(value)); | ||
} else { | ||
newArray.push(value); | ||
} | ||
} | ||
/** | ||
* Checks if two arrays are equal regardless of object reference. | ||
*/ | ||
public static isEqual(array1: Array<any>, array2: Array<any>): boolean { | ||
return ArrayUtil.getDifference(array1, array2).length === 0; | ||
} | ||
/** | ||
* Checks if two arrays are equal regardless of object reference. | ||
*/ | ||
public static isEqual(array1: Array<any>, array2: Array<any>): boolean { | ||
return ArrayUtil.getDifference(array1, array2).length === 0; | ||
/** | ||
* Returns an array of differences as `Difference` objects. | ||
*/ | ||
public static getDifference( | ||
array1: Array<any>, | ||
array2: Array<any> | ||
): Array<any> { | ||
const differences = new Array<Difference>(); | ||
if ( | ||
EmptyUtil.isNullOrUndefined(array1) || | ||
EmptyUtil.isNullOrUndefined(array2) | ||
) { | ||
differences.push(new Difference('array', array1, array2)); | ||
} else { | ||
array1.forEach((n, i) => | ||
CompareUtil.setDifferences(i, array1[i], array2[i], differences) | ||
); | ||
} | ||
return differences; | ||
} | ||
/** | ||
* Returns an array of differences as `Difference` objects. | ||
*/ | ||
public static getDifference(array1: Array<any>, array2: Array<any>): Array<any> { | ||
const differences = new Array<Difference>(); | ||
/** | ||
* Removes any duplicate entries from an array. | ||
* May fail, if you provide it with anything that can't be JSON.stringified | ||
*/ | ||
public static removeDuplicates( | ||
fromArray: any[], | ||
immutable: boolean = false | ||
): void | any[] { | ||
const map = {}; | ||
const removeIndexes = []; | ||
const newArray = []; | ||
fromArray.forEach((value, index) => { | ||
const id = JSON.stringify(value); | ||
if (!map[id]) { | ||
map[id] = true; | ||
newArray.push(value); | ||
} else if (!immutable) { | ||
removeIndexes.push(index); | ||
} | ||
}); | ||
if (EmptyUtil.isNullOrUndefined(array1) || EmptyUtil.isNullOrUndefined(array2)) { | ||
differences.push(new Difference('array', array1, array2)); | ||
} else { | ||
array1.forEach((n, i) => | ||
CompareUtil.setDifferences(i, array1[i], array2[i], differences)); | ||
} | ||
return differences; | ||
if (!immutable) { | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
} else { | ||
return newArray; | ||
} | ||
} | ||
/** | ||
* Removes any duplicate entries from an array. | ||
* May fail, if you provide it with anything that can't be JSON.stringified | ||
*/ | ||
public static removeDuplicates(fromArray: any[]) { | ||
const map = {}; | ||
const removeIndexes = []; | ||
fromArray.forEach((value, index) => { | ||
const id = JSON.stringify(value); | ||
if (map[id]) { | ||
removeIndexes.push(index); | ||
} else { | ||
map[id] = true; | ||
} | ||
}); | ||
/** | ||
* Removes all entries that is identical to a value from an array, | ||
* regardless of the object reference. | ||
* @param value The value to be removed from the array. | ||
* @param fromArray | ||
*/ | ||
public static removeObject( | ||
value: any, | ||
fromArray: any[], | ||
immutable: boolean = false | ||
): void | any[] { | ||
const removeIndexes = []; | ||
const newArray = []; | ||
fromArray.forEach((v, index) => { | ||
let equal = false; | ||
if (ObjectUtil.isObject(value) && ObjectUtil.isEqual(v, value)) { | ||
equal = true; | ||
} else if (ArrayUtil.isArray(value) && ArrayUtil.isEqual(v, value)) { | ||
equal = true; | ||
} else if (v === value) { | ||
equal = true; | ||
} | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
if (equal && !immutable) { | ||
removeIndexes.push(index); | ||
} else if (immutable) { | ||
newArray.push(v); | ||
} | ||
}); | ||
if (!immutable) { | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
} else { | ||
return newArray; | ||
} | ||
} | ||
/** | ||
* Removes all entries that is identical to a value from an array, | ||
* regardless of the object reference. | ||
* @param value The value to be removed from the array. | ||
* @param fromArray | ||
*/ | ||
public static removeObject(value: any, fromArray: any[]): void { | ||
const removeIndexes = []; | ||
fromArray.forEach((v, index) => { | ||
let equal = false; | ||
if (ObjectUtil.isObject(value) && ObjectUtil.isEqual(v, value)) { | ||
equal = true; | ||
} else if (ArrayUtil.isArray(value) && ArrayUtil.isEqual(v, value)) { | ||
equal = true; | ||
} else if (v === value) { | ||
equal = true; | ||
} | ||
/** | ||
* Removes multiple objects/values from an array. | ||
* @param values A list of objects or values to be removed | ||
* @param fromArray | ||
*/ | ||
public static removeObjects(values: any[], fromArray: any[]): void { | ||
values.forEach(value => ArrayUtil.removeObject(value, fromArray)); | ||
} | ||
if (equal) { | ||
removeIndexes.push(index); | ||
} | ||
}); | ||
ArrayUtil.removeIndexes(removeIndexes, fromArray); | ||
} | ||
/** | ||
* Removes multiple objects/values from an array. | ||
* @param values A list of objects or values to be removed | ||
* @param fromArray | ||
*/ | ||
public static removeObjects(values: any[], fromArray: any[]): void { | ||
values.forEach(value => | ||
ArrayUtil.removeObject(value, fromArray)); | ||
} | ||
/** | ||
* Removes the elements from an array at the supplied index locations | ||
*/ | ||
public static removeIndexes(indexes: number[], fromArray: any[]): void { | ||
indexes.sort((a, b) => b - a).forEach(i => fromArray.splice(i, 1)); | ||
} | ||
/** | ||
* Removes the elements from an array at the supplied index locations | ||
*/ | ||
public static removeIndexes(indexes: number[], fromArray: any[]): void { | ||
indexes.sort( | ||
(a, b) => b - a) | ||
.forEach(i => | ||
fromArray.splice(i, 1)); | ||
} | ||
/** | ||
* Returns an array with the items in the provided array in random order. | ||
* @param array The array | ||
*/ | ||
public static randomOrder(array: any[]): any[] { | ||
const random = []; | ||
const tmp = [...array]; | ||
for (let i = 0, length = array.length; i < length; i++) { | ||
let index = Math.round(Math.random() * tmp.length - 1); | ||
/** | ||
* Returns an array with the items in the provided array in random order. | ||
* @param array The array | ||
*/ | ||
public static randomOrder(array: any[]): any[] { | ||
const random = []; | ||
const tmp = [...array]; | ||
for(let i = 0, length = array.length; i < length; i++) { | ||
let index = Math.round(Math.random() * tmp.length - 1); | ||
if (index < 0) { | ||
index = 0; | ||
} | ||
if (index < 0) { | ||
index = 0; | ||
} | ||
random.push(tmp[index]); | ||
ArrayUtil.removeIndexes([index], tmp); | ||
} | ||
return random; | ||
random.push(tmp[index]); | ||
ArrayUtil.removeIndexes([index], tmp); | ||
} | ||
return random; | ||
} | ||
} |
@@ -97,5 +97,5 @@ import {ObjectUtil} from './object.util'; | ||
expect(ObjectUtil.isEqual(obj1, obj2)).toBeFalsy(); | ||
expect(ObjectUtil.getDifference(document, document).length).toBeFalsy(); | ||
expect(ObjectUtil.getDifference(obj2, obj2).length).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
@@ -5,15 +5,5 @@ { | ||
"outDir": "./out-tsc/spec", | ||
"types": [ | ||
"jasmine", | ||
"node" | ||
] | ||
"types": ["jasmine", "node"] | ||
}, | ||
"files": [ | ||
"src/test.ts", | ||
"src/polyfills.ts" | ||
], | ||
"include": [ | ||
"src/**/*.spec.ts", | ||
"src/**/*.d.ts" | ||
] | ||
"include": ["src/**/*.spec.ts", "src/**/*.d.ts"] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8
201437
82
2963