Object calculating fields
Summary
This package contains generators of calculating fields for manipulations with objects.
API
- Enlarge
- GetField
- GetFieldsArray
- GetRequiredFieldNames
- UpdateWithPattern
- ZipFieldNamesAndValuesArrays
API methods clarification
1. Enlarge
Signature
/* arguments object */
({
partToBeEnlargedWith: object,
//true by default
shouldWorkWithInvalidSource?: boolean
})
/* returns */
(targetObject: object) => object
Usage example
const { Enlarge } = require("thesmo-calc-fields-object");
const testEnlarger = Enlarge({ partToBeEnlargedWith: {a: 1, b: 2} });
const sourceData_1 = {g: 3, a: 4};
const sourceData_2 = {c: 5, d: 6};
// {a: 1, b: 2, g: 3}
const enlargedData_1 = testEnlarger(sourceData_1);
// {a: 1, b: 2, c: 5, d: 6}
const enlargedData_2 = testEnlarger(sourceData_2);
2. GetField
Signature
/* arguments object */
({
path: string
})
/* returns */
(targetObject: object) => any
Usage example
const { GetField } = require("thesmo-calc-fields-object");
const testObject = {a: {b: false, c: undefined}};
// {b: false, c: undefined}
const a = GetField({ path: "a" })(testObject);
// false
const a_b = GetField({ path: "a.b" })(testObject);
// undefined
const a_c = GetField({ path: "a.c" })(testObject);
// undefined
const non_existing_field = GetField({ path: "non_existing_field" })(testObject);
3. GetFieldsArray
Signature
/* arguments object */
({
fieldNames: string[]
})
/* returns */
(targetObject: object) => any[]
Usage example
const { GetFieldsArray } = require("thesmo-calc-fields-object");
const testFieldValuesObtainer = GetFieldsArray({
fieldNames: ["b", "e", "f", "d", "d.a_nested"]
});
const sourceObject = {
a: 1,
b: 15,
c: true,
d: { a_nested: 20 },
e: "str",
f: 6
};
// [15, "str", 6, { a_nested: 20 }, 20]
const requiredFieldValues = testFieldValuesObtainer(sourceObject);
4. GetRequiredFieldNames
Signature
/* arguments object */
({
//(memberUnderCheck) => true by default
requiredMemberCheck?: (memberUnderCheck: *, fieldName: string) => boolean
})
/* returns */
(targetObject: object) => string[]
Usage example
const { GetRequiredFieldNames } = require("thesmo-calc-fields-object");
const testFieldNamesObtainer = GetRequiredFieldNames({
requiredMemberCheck: (x, fieldName) => x > 5 || typeof x === "string" || fieldName.length > 1
});
const sourceObject = {
a: 1,
b: 15,
c: true,
d: { a_nested: 20 },
e: "str",
f: 6,
abc: 0
};
// ["b", "e", "f", "abc"]
const requiredFieldNames = testFieldNamesObtainer(sourceObject);
5. UpdateWithPattern
Signature
/* arguments object */
({
// values can be strict or calculating fields
pattern: Object,
//true by default
shouldWorkWithInvalidSource?: boolean
})
/* returns */
(targetObject: object) => object
Usage example
const { UpdateWithPattern } = require("thesmo-calc-fields-object");
const testPatternUpdater = UpdateWithPattern({
pattern: {
foo: 15,
bar: (x) => x + 14
}
});
const sourceObject = {
bar: 1,
baz: true
};
// { bar: 15, baz: true, foo: 15 }
const updatedValue = testPatternUpdater(sourceObject);
6. ZipFieldNamesAndValuesArrays
Signature
/* arguments object */
({
fieldNamesArray: string[],
valuesArray: any[],
//true by default
shouldWorkWithInvalidSource?: boolean
})
/* returns */
(targetObject: object) => object
Usage example
const { ZipFieldNamesAndValuesArrays } = require("thesmo-calc-fields-object");
const testZipper = ZipFieldNamesAndValuesArrays({
fieldNamesArray: ["a", "b", "c"],
valuesArray: [5, 10, {hello: "world"}]
});
const sourceObject = {
bar: 1,
baz: true
};
// { bar: 1, baz: true, a: 5, b: 10, c: { hello: "world" } }
const zippedValue = testZipper(sourceObject);