@ukon1990/js-utilities
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -8,3 +8,3 @@ "use strict"; | ||
static isObject(value) { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
@@ -14,3 +14,3 @@ !value.forEach && !value.push; | ||
static isArray(value) { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
@@ -20,3 +20,3 @@ value.forEach && value.push; | ||
static isAPopulatedObject(value) { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
@@ -26,3 +26,3 @@ !ObjectUtil.isArray(value); | ||
static overwrite(from, to) { | ||
if (ObjectUtil.isNullOrUndefined(from)) { | ||
if (EmptyUtil.isNullOrUndefined(from)) { | ||
console.error('Could not overwrite an object because the source is null or undefined'); | ||
@@ -48,3 +48,3 @@ return; | ||
static clone(object) { | ||
if (ObjectUtil.isNullOrUndefined(object)) { | ||
if (EmptyUtil.isNullOrUndefined(object)) { | ||
console.error('Could not clone an object because the source is null or undefined'); | ||
@@ -68,3 +68,3 @@ return object; | ||
static cloneArray(array) { | ||
if (ObjectUtil.isNullOrUndefined(array)) { | ||
if (EmptyUtil.isNullOrUndefined(array)) { | ||
console.error('Could not clone an array because it is null or undefined'); | ||
@@ -71,0 +71,0 @@ return array; |
@@ -7,3 +7,3 @@ "use strict"; | ||
static contains(source, target) { | ||
if (object_util_1.ObjectUtil.isNullOrUndefined(source) || object_util_1.ObjectUtil.isNullOrUndefined(target)) { | ||
if (object_util_1.EmptyUtil.isNullOrUndefined(source) || object_util_1.EmptyUtil.isNullOrUndefined(target)) { | ||
return false; | ||
@@ -10,0 +10,0 @@ } |
{ | ||
"name": "@ukon1990/js-utilities", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A light weight package for object and array manipulation. As well as some utilities for matching text.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import {ObjectUtil} from './object.util'; | ||
import {Difference} from "../models/difference.model"; | ||
import {EmptyUtil} from "./empty.util"; | ||
export class CompareUtil { | ||
public static objectEquality(object1: Object, object2: Object): boolean { | ||
return CompareUtil.objectDifference(object1, object2).length === 0; | ||
} | ||
public static objectDifference(object1: object | any, object2: object | any, ignoreFields?: any): Array<Difference> { | ||
const differences = new Array<Difference>(); | ||
if (!object1 || object1 === null || !object2 || object2 === null) { | ||
differences.push(new Difference('array', object1, object2)); | ||
} else { | ||
Object.keys(object1).forEach(n => { | ||
if (ignoreFields && ignoreFields[n]) { | ||
return; | ||
} | ||
CompareUtil.setDifferences(n, object1[n], object2[n], differences); | ||
}); | ||
} | ||
return differences; | ||
} | ||
public static arrayEquality(array1: Array<any>, array2: Array<any>): boolean { | ||
return CompareUtil.arrayDifference(array1, array2).length === 0; | ||
} | ||
public static arrayDifference(array1: Array<any>, array2: Array<any>): Array<any> { | ||
const differences = new Array<Difference>(); | ||
if (!array1 || array1 === null || !array2 || array2 === null) { | ||
differences.push(new Difference('array', array1, array2)); | ||
} else { | ||
array1.forEach((n, i) => | ||
CompareUtil.setDifferences(i, array1[i], array2[i], differences)); | ||
} | ||
return differences; | ||
} | ||
private static setDifferences(key: any, value1: any, value2: any, differences: Array<Difference>): void { | ||
static setDifferences(key: any, value1: any, value2: any, differences: Array<Difference>): void { | ||
if (CompareUtil.isNullAndUndefined(value1, value2)) { | ||
@@ -48,3 +13,3 @@ return; | ||
CompareUtil.handleNullOrUndefined(key, value1, value2, differences); | ||
} else if (ObjectUtil.isAPopulatedObject(value1)) { | ||
} else if (ObjectUtil.isObject(value1)) { | ||
const childDifference = new Difference( | ||
@@ -51,0 +16,0 @@ key, value1, value2, CompareUtil.arrayDifference(value1, value2)); |
@@ -0,9 +1,10 @@ | ||
import {EmptyUtil} from "./empty.util"; | ||
import {ArrayUtil} from "./array.util"; | ||
import {Difference} from "../models/difference.model"; | ||
import {CompareUtil} from "./compare.util"; | ||
export class ObjectUtil { | ||
public static isNullOrUndefined(value: any): boolean { | ||
return !value || value === null; | ||
} | ||
public static isObject(value: any): boolean { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
@@ -13,16 +14,4 @@ !value.forEach && !value.push; | ||
public static isArray(value: any): boolean { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
value.forEach && value.push; | ||
} | ||
public static isAPopulatedObject(value: any): boolean { | ||
return !ObjectUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
!ObjectUtil.isArray(value); | ||
} | ||
public static overwrite(from: object | any, to: object | any): void { | ||
if (ObjectUtil.isNullOrUndefined(from)) { | ||
if (EmptyUtil.isNullOrUndefined(from)) { | ||
console.error( 'Could not overwrite an object because the source is null or undefined'); | ||
@@ -33,17 +22,21 @@ return; | ||
Object.keys(from).forEach(key => { | ||
if (ObjectUtil.isAPopulatedObject(from[key]) && to) { | ||
to[key] = ObjectUtil.cloneArray(from[key]); | ||
} else if (ObjectUtil.isObject(from[key]) && to) { | ||
to[key] = ObjectUtil.overwrite(from[key], to[key]); | ||
} else { | ||
try { | ||
to[key] = from[key]; | ||
} catch (e) { | ||
} | ||
} | ||
ObjectUtil.overwriteField(from, key, to); | ||
}); | ||
} | ||
private static overwriteField(from: object | any, key: string, to: object | any) { | ||
if (ArrayUtil.isArray(from[key]) && to) { | ||
to[key] = ObjectUtil.clone(from[key]); | ||
} else if (ObjectUtil.isObject(from[key]) && to) { | ||
to[key] = ObjectUtil.overwrite(from[key], to[key]); | ||
} else { | ||
try { | ||
to[key] = from[key]; | ||
} catch (e) { | ||
} | ||
} | ||
} | ||
public static clone(object: any): object { | ||
if (ObjectUtil.isNullOrUndefined(object)) { | ||
if (EmptyUtil.isNullOrUndefined(object)) { | ||
console.error('Could not clone an object because the source is null or undefined'); | ||
@@ -55,9 +48,3 @@ return object; | ||
Object.keys(object).forEach(key => { | ||
if (ObjectUtil.isAPopulatedObject(object[key])) { | ||
obj[key] = ObjectUtil.cloneArray(object[key]); | ||
} else if (ObjectUtil.isObject(object[key])) { | ||
obj[key] = ObjectUtil.clone(object[key]); | ||
} else { | ||
obj[key] = object[key]; | ||
} | ||
ObjectUtil.cloneField(object, key, obj); | ||
}); | ||
@@ -67,20 +54,30 @@ return obj; | ||
public static cloneArray(array: Array<any>): Array<any> { | ||
if (ObjectUtil.isNullOrUndefined(array)) { | ||
console.error('Could not clone an array because it is null or undefined'); | ||
return array; | ||
private static cloneField(object: any, key: string, obj: any) { | ||
if (ArrayUtil.isArray(object[key])) { | ||
obj[key] = ArrayUtil.clone(object[key]); | ||
} else if (ObjectUtil.isObject(object[key])) { | ||
obj[key] = ObjectUtil.clone(object[key]); | ||
} else { | ||
obj[key] = object[key]; | ||
} | ||
} | ||
const newArray = Array<any>(); | ||
array.forEach(value => { | ||
if (ObjectUtil.isAPopulatedObject(value)) { | ||
newArray.push(ObjectUtil.cloneArray(value)); | ||
} else if (ObjectUtil.isObject(value)) { | ||
newArray.push(ObjectUtil.clone(value)); | ||
} else { | ||
newArray.push(value); | ||
} | ||
}); | ||
return newArray; | ||
public static isEqual(object1: Object, object2: Object): boolean { | ||
return ObjectUtil.getDifference(object1, object2).length === 0; | ||
} | ||
public static getDifference(object1: object | any, object2: object | any, ignoreFields?: any): Array<Difference> { | ||
const differences = new Array<Difference>(); | ||
if (EmptyUtil.isNullOrUndefined(object1) || EmptyUtil.isNullOrUndefined(object2)) { | ||
differences.push(new Difference('array', object1, object2)); | ||
} else { | ||
Object.keys(object1).forEach(n => { | ||
if (ignoreFields && ignoreFields[n]) { | ||
return; | ||
} | ||
CompareUtil.setDifferences(n, object1[n], object2[n], differences); | ||
}); | ||
} | ||
return differences; | ||
} | ||
} |
import {Match} from '../models/match.model'; | ||
import {ObjectUtil} from "./object.util"; | ||
import {EmptyUtil} from "./empty.util"; | ||
@@ -7,3 +7,3 @@ export class TextUtil { | ||
public static contains(source: string, target: string): boolean { | ||
if (ObjectUtil.isNullOrUndefined(source) || ObjectUtil.isNullOrUndefined(target)) { | ||
if (EmptyUtil.isNullOrUndefined(source) || EmptyUtil.isNullOrUndefined(target)) { | ||
return false; | ||
@@ -21,15 +21,31 @@ } | ||
public static matchingParts(string: string, matchingString: string): Match { | ||
public static getMatchingParts(string: string, matchingString: string): Match { | ||
const match = new Match('', '', ''); | ||
let firstIndex = TextUtil.getIndexOf(string, matchingString); | ||
let firstIndex = 0; | ||
if (!string) { | ||
if (EmptyUtil.isNullOrUndefined(string)) { | ||
return match; | ||
} | ||
firstIndex = TextUtil.setFirstMatchPartAndIndex( | ||
firstIndex, string, matchingString, match); | ||
TextUtil.setMatchingParts( | ||
firstIndex, string, match, matchingString); | ||
return match; | ||
} | ||
private static setFirstMatchPartAndIndex(firstIndex: number, string: string, matchingString: string, match) { | ||
firstIndex = TextUtil.getIndexOf(string, matchingString); | ||
if (firstIndex === -1) { | ||
firstIndex = 0; | ||
} | ||
match.start = string.slice(0, firstIndex); | ||
return firstIndex; | ||
} | ||
private static setMatchingParts(firstIndex: number, string: string, match: Match, matchingString: string) { | ||
for (let i = firstIndex, x = string.length; i < x; i++) { | ||
@@ -42,3 +58,2 @@ if (match.match.toLowerCase() === matchingString.toLowerCase()) { | ||
} | ||
return match; | ||
} | ||
@@ -45,0 +60,0 @@ |
62100
21
572