Renamed form.reducers.ts to form.reducer.ts

Created custom sass variable
Removed use of instanceof operator
This commit is contained in:
Giuseppe Digilio
2018-07-03 17:51:26 +02:00
parent b74993413d
commit ff41b80a33
29 changed files with 220 additions and 137 deletions

View File

@@ -4,21 +4,21 @@ import { isEqual, isObject, transform } from 'lodash';
/**
* Returns passed object without specified property
*/
export function deleteProperty(object, key): object {
export function deleteProperty(object: object, key: string): object {
const {[key]: deletedKey, ...otherKeys} = object;
return otherKeys;
}
/**
* Returns true if the passed object is empty or has only empty property.
* isObjectEmpty({}); // true
* isObjectEmpty({a: null}); // true
* isObjectEmpty({a: []}); // true
* isObjectEmpty({a: [], b: {}); // true
* isObjectEmpty({a: 'a', b: 'b'}); // false
* isObjectEmpty({a: [], b: 'b'}); // false
* hasOnlyEmptyProperties({}); // true
* hasOnlyEmptyProperties({a: null}); // true
* hasOnlyEmptyProperties({a: []}); // true
* hasOnlyEmptyProperties({a: [], b: {}); // true
* hasOnlyEmptyProperties({a: 'a', b: 'b'}); // false
* hasOnlyEmptyProperties({a: [], b: 'b'}); // false
*/
export function isObjectEmpty(obj: any): boolean {
export function hasOnlyEmptyProperties(obj: object): boolean {
const objectType = typeof obj;
if (objectType === 'object') {
if (Object.keys(obj).length === 0) {
@@ -43,12 +43,12 @@ export function isObjectEmpty(obj: any): boolean {
* difference({a: 'a', b: {}}, {a: 'a'}); // {}
* difference({a: 'a'}, {a: 'a', b: 'b'}); // {}
*/
export function difference(object, base) {
export function difference(object: object, base: object) {
const changes = (o, b) => {
return transform(o, (result, value, key) => {
if (!isEqual(value, b[key]) && isNotEmpty(value)) {
const resultValue = (isObject(value) && isObject(b[key])) ? changes(value, b[key]) : value;
if (!isObjectEmpty(resultValue)) {
if (!hasOnlyEmptyProperties(resultValue)) {
result[key] = resultValue;
}
}