@ukon1990/js-utilities
Advanced tools
Comparing version 0.0.2 to 0.0.3
import { Difference } from "../models/difference.model"; | ||
export declare class CompareUtil { | ||
static objectEquality(object1: Object, object2: Object): boolean; | ||
static objectDifference(object1: object | any, object2: object | any, ignoreFields?: any): Array<Difference>; | ||
static arrayEquality(array1: Array<any>, array2: Array<any>): boolean; | ||
static arrayDifference(array1: Array<any>, array2: Array<any>): Array<any>; | ||
private static setDifferences; | ||
static setDifferences(key: any, value1: any, value2: any, differences: Array<Difference>): void; | ||
private static handleNullOrUndefined; | ||
@@ -9,0 +5,0 @@ private static hasChildren; |
@@ -5,34 +5,4 @@ "use strict"; | ||
const difference_model_1 = require("../models/difference.model"); | ||
const array_util_1 = require("./array.util"); | ||
class CompareUtil { | ||
static objectEquality(object1, object2) { | ||
return CompareUtil.objectDifference(object1, object2).length === 0; | ||
} | ||
static objectDifference(object1, object2, ignoreFields) { | ||
const differences = new Array(); | ||
if (!object1 || object1 === null || !object2 || object2 === null) { | ||
differences.push(new difference_model_1.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; | ||
} | ||
static arrayEquality(array1, array2) { | ||
return CompareUtil.arrayDifference(array1, array2).length === 0; | ||
} | ||
static arrayDifference(array1, array2) { | ||
const differences = new Array(); | ||
if (!array1 || array1 === null || !array2 || array2 === null) { | ||
differences.push(new difference_model_1.Difference('array', array1, array2)); | ||
} | ||
else { | ||
array1.forEach((n, i) => CompareUtil.setDifferences(i, array1[i], array2[i], differences)); | ||
} | ||
return differences; | ||
} | ||
static setDifferences(key, value1, value2, differences) { | ||
@@ -45,4 +15,4 @@ if (CompareUtil.isNullAndUndefined(value1, value2)) { | ||
} | ||
else if (object_util_1.ObjectUtil.isAPopulatedObject(value1)) { | ||
const childDifference = new difference_model_1.Difference(key, value1, value2, CompareUtil.arrayDifference(value1, value2)); | ||
else if (object_util_1.ObjectUtil.isObject(value1)) { | ||
const childDifference = new difference_model_1.Difference(key, value1, value2, array_util_1.ArrayUtil.getDifference(value1, value2)); | ||
if (CompareUtil.hasChildren(childDifference)) { | ||
@@ -53,3 +23,3 @@ differences.push(childDifference); | ||
else if (object_util_1.ObjectUtil.isObject(value1)) { | ||
const childDifference = new difference_model_1.Difference(key, value1, value2, CompareUtil.objectDifference(value1, value2)); | ||
const childDifference = new difference_model_1.Difference(key, value1, value2, object_util_1.ObjectUtil.getDifference(value1, value2)); | ||
if (CompareUtil.hasChildren(childDifference)) { | ||
@@ -56,0 +26,0 @@ differences.push(childDifference); |
@@ -0,9 +1,10 @@ | ||
import { Difference } from "../models/difference.model"; | ||
export declare class ObjectUtil { | ||
static isNullOrUndefined(value: any): boolean; | ||
static isObject(value: any): boolean; | ||
static isArray(value: any): boolean; | ||
static isAPopulatedObject(value: any): boolean; | ||
static overwrite(from: object | any, to: object | any): void; | ||
private static overwriteField; | ||
static clone(object: any): object; | ||
static cloneArray(array: Array<any>): Array<any>; | ||
private static cloneField; | ||
static isEqual(object1: Object, object2: Object): boolean; | ||
static getDifference(object1: object | any, object2: object | any, ignoreFields?: any): Array<Difference>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const empty_util_1 = require("./empty.util"); | ||
const array_util_1 = require("./array.util"); | ||
const difference_model_1 = require("../models/difference.model"); | ||
const compare_util_1 = require("./compare.util"); | ||
class ObjectUtil { | ||
static isNullOrUndefined(value) { | ||
return !value || value === null; | ||
} | ||
static isObject(value) { | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
return !empty_util_1.EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
!value.forEach && !value.push; | ||
} | ||
static isArray(value) { | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
value.forEach && value.push; | ||
} | ||
static isAPopulatedObject(value) { | ||
return !EmptyUtil.isNullOrUndefined(value) && | ||
typeof value === 'object' && | ||
!ObjectUtil.isArray(value); | ||
} | ||
static overwrite(from, to) { | ||
if (EmptyUtil.isNullOrUndefined(from)) { | ||
if (empty_util_1.EmptyUtil.isNullOrUndefined(from)) { | ||
console.error('Could not overwrite an object because the source is null or undefined'); | ||
@@ -28,19 +19,22 @@ return; | ||
Object.keys(from).forEach(key => { | ||
if (ObjectUtil.isAPopulatedObject(from[key]) && to) { | ||
to[key] = ObjectUtil.cloneArray(from[key]); | ||
ObjectUtil.overwriteField(from, key, to); | ||
}); | ||
} | ||
static overwriteField(from, key, to) { | ||
if (array_util_1.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]; | ||
} | ||
else if (ObjectUtil.isObject(from[key]) && to) { | ||
to[key] = ObjectUtil.overwrite(from[key], to[key]); | ||
catch (e) { | ||
} | ||
else { | ||
try { | ||
to[key] = from[key]; | ||
} | ||
catch (e) { | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
static clone(object) { | ||
if (EmptyUtil.isNullOrUndefined(object)) { | ||
if (empty_util_1.EmptyUtil.isNullOrUndefined(object)) { | ||
console.error('Could not clone an object because the source is null or undefined'); | ||
@@ -51,34 +45,36 @@ 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); | ||
}); | ||
return obj; | ||
} | ||
static cloneArray(array) { | ||
if (EmptyUtil.isNullOrUndefined(array)) { | ||
console.error('Could not clone an array because it is null or undefined'); | ||
return array; | ||
static cloneField(object, key, obj) { | ||
if (array_util_1.ArrayUtil.isArray(object[key])) { | ||
obj[key] = array_util_1.ArrayUtil.clone(object[key]); | ||
} | ||
const newArray = Array(); | ||
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; | ||
else if (ObjectUtil.isObject(object[key])) { | ||
obj[key] = ObjectUtil.clone(object[key]); | ||
} | ||
else { | ||
obj[key] = object[key]; | ||
} | ||
} | ||
static isEqual(object1, object2) { | ||
return ObjectUtil.getDifference(object1, object2).length === 0; | ||
} | ||
static getDifference(object1, object2, ignoreFields) { | ||
const differences = new Array(); | ||
if (empty_util_1.EmptyUtil.isNullOrUndefined(object1) || empty_util_1.EmptyUtil.isNullOrUndefined(object2)) { | ||
differences.push(new difference_model_1.Difference('array', object1, object2)); | ||
} | ||
else { | ||
Object.keys(object1).forEach(n => { | ||
if (ignoreFields && ignoreFields[n]) { | ||
return; | ||
} | ||
compare_util_1.CompareUtil.setDifferences(n, object1[n], object2[n], differences); | ||
}); | ||
} | ||
return differences; | ||
} | ||
} | ||
exports.ObjectUtil = ObjectUtil; |
@@ -5,4 +5,6 @@ import { Match } from '../models/match.model'; | ||
static getIndexOf(source: string, target: string): number; | ||
static matchingParts(string: string, matchingString: string): Match; | ||
static getMatchingParts(string: string, matchingString: string): Match; | ||
private static setFirstMatchPartAndIndex; | ||
private static setMatchingParts; | ||
static isLowerCase(text: string): boolean; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const match_model_1 = require("../models/match.model"); | ||
const object_util_1 = require("./object.util"); | ||
const empty_util_1 = require("./empty.util"); | ||
class TextUtil { | ||
static contains(source, target) { | ||
if (object_util_1.EmptyUtil.isNullOrUndefined(source) || object_util_1.EmptyUtil.isNullOrUndefined(target)) { | ||
if (empty_util_1.EmptyUtil.isNullOrUndefined(source) || empty_util_1.EmptyUtil.isNullOrUndefined(target)) { | ||
return false; | ||
@@ -18,8 +18,14 @@ } | ||
} | ||
static matchingParts(string, matchingString) { | ||
static getMatchingParts(string, matchingString) { | ||
const match = new match_model_1.Match('', '', ''); | ||
let firstIndex = TextUtil.getIndexOf(string, matchingString); | ||
if (!string) { | ||
let firstIndex = 0; | ||
if (empty_util_1.EmptyUtil.isNullOrUndefined(string)) { | ||
return match; | ||
} | ||
firstIndex = TextUtil.setFirstMatchPartAndIndex(firstIndex, string, matchingString, match); | ||
TextUtil.setMatchingParts(firstIndex, string, match, matchingString); | ||
return match; | ||
} | ||
static setFirstMatchPartAndIndex(firstIndex, string, matchingString, match) { | ||
firstIndex = TextUtil.getIndexOf(string, matchingString); | ||
if (firstIndex === -1) { | ||
@@ -29,2 +35,5 @@ firstIndex = 0; | ||
match.start = string.slice(0, firstIndex); | ||
return firstIndex; | ||
} | ||
static setMatchingParts(firstIndex, string, match, matchingString) { | ||
for (let i = firstIndex, x = string.length; i < x; i++) { | ||
@@ -38,3 +47,2 @@ if (match.match.toLowerCase() === matchingString.toLowerCase()) { | ||
} | ||
return match; | ||
} | ||
@@ -41,0 +49,0 @@ static isLowerCase(text) { |
{ | ||
"name": "@ukon1990/js-utilities", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A light weight package for object and array manipulation. As well as some utilities for matching text.", | ||
@@ -10,3 +10,3 @@ "main": "dist/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"publish": "npm publish --access public" | ||
"deploy": "tsc && npm publish --access public" | ||
}, | ||
@@ -13,0 +13,0 @@ "keywords": [ |
import {ObjectUtil} from './object.util'; | ||
import {Difference} from "../models/difference.model"; | ||
import {EmptyUtil} from "./empty.util"; | ||
import {ArrayUtil} from "./array.util"; | ||
@@ -15,3 +16,3 @@ export class CompareUtil { | ||
const childDifference = new Difference( | ||
key, value1, value2, CompareUtil.arrayDifference(value1, value2)); | ||
key, value1, value2, ArrayUtil.getDifference(value1, value2)); | ||
@@ -23,3 +24,3 @@ if (CompareUtil.hasChildren(childDifference)) { | ||
const childDifference = new Difference( | ||
key, value1, value2, CompareUtil.objectDifference(value1, value2)); | ||
key, value1, value2, ObjectUtil.getDifference(value1, value2)); | ||
@@ -26,0 +27,0 @@ if (CompareUtil.hasChildren(childDifference)) { |
@@ -37,3 +37,4 @@ import {Match} from '../models/match.model'; | ||
private static setFirstMatchPartAndIndex(firstIndex: number, string: string, matchingString: string, match) { | ||
private static setFirstMatchPartAndIndex( | ||
firstIndex: number, string: string, matchingString: string, match: Match) { | ||
firstIndex = TextUtil.getIndexOf(string, matchingString); | ||
@@ -49,3 +50,4 @@ | ||
private static setMatchingParts(firstIndex: number, string: string, match: Match, matchingString: string) { | ||
private static setMatchingParts( | ||
firstIndex: number, string: string, match: Match, matchingString: string) { | ||
for (let i = firstIndex, x = string.length; i < x; i++) { | ||
@@ -52,0 +54,0 @@ if (match.match.toLowerCase() === matchingString.toLowerCase()) { |
64157
25
623