Data - globules-io/OGX.JS GitHub Wiki
OGX.Data
is a static helper class to manipulate, convert data. There is no need to create an instance of this class.
Methods
OGX.Data.merge(__master, __slave, __overwrite, __copy, __strict);
OGX.Data.weld(__master, __slave, __overwrite, __copy);
OGX.Data.diff(__master, __slave, __strict);
OGX.Data.props(__obj, __array);
OGX.Data.clone(__obj);
OGX.Data.isFloat(__val);
OGX.Data.isInt(__val);
OGX.Data.isSizeExp(__string);
OGX.Data.toSizeExp(__string_or_number);
OGX.Data.addDec(__n);
OGX.Data.pad(__n, __length, __direction, __string);
OGX.Data.stringToVal(__string);
OGX.Data.dateToLocale(__date|__string, __options);
OGX.Data.getBestSize(__dimension, __value);
OGX.Data.same(__objectA, __objectB, __deep);
OGX.Data.eval(__obj, __prop, __val);
OGX.Data.clipboard(__string);
Merge
The
merge
method merges two objects into one, by appending to the master object the slave's properties . If the__override
flag is set totrue
and the same properties are found in both objects, the slave's properties/values will override the master's. The__copy
flag, if set totrue
, returns a copy of the master object, leaving the master object untouched. If the__strict
flag is set totrue
, only existing properties of the master slave will be updated.
let new_object = OGX.Data.merge(objectA, objectB, true, true, false);
Note that merge will only consider a single level/depth of properties. For deep merging, use the
weld
method.
Weld
Similar to
merge
but with deep update asmerge
only supports a single depth. Theweld
method does not support the__strict
flag.
let new_object = OGX.Data.weld(objectA, objectB, true, true);
1.9.5+
diff Retrieve an object composed of the values that are different between a master and a slave object
let diff = OGX.Data.diff(objectA, objectB, true);
Props
Use this method to check if an object has the required properties. Returns
true
orfalse
.
let bool = OGX.Data.props(objectA, ['age', 'gender', 'weight']);
Clone
A deep cloning method, shortcut to stringify/parse via JSON.
let new_object = OGX.Data.clone(objectA);
isFloat
Test if a variable is a float
const bool = OGX.Data.isFloat(myvar);
isInt
Test if a variable is an integer
const bool = OGX.Data.isInt(myvar);
1.29.0+
isSizeExp Test if a variable is a size expression
const bool = OGX.Data.isSizeExp(myvar);
1.29.0+
toSizeExp Converts a dimension to a size expression,
min
andmax
are optional
const exp = OGX.Data.toSizeExp(myvar, min, max);
addDec
Convert a number to a string then pads up to 2 decimals
console.log(OGX.Data.addDec(654)); // '654.00'
pad
Pad a string with a given character until a certain overall length is reached
console.log(OGX.Data.pad('654', 5, -1, '0'); // '00654'
console.log(OGX.Data.pad('654', 5, 1, '0'); // '65400'
stringToVal
Auto convert a string to appropriate type.
let val = OGX.Data.stringToVal(__string);
OGX.Data.stringToVal('true'); //return true
OGX.Data.stringToVal('false'); //return false
OGX.Data.stringToVal('1'); //return 1
dateToLocale
Converts a date or a parse-ale string into a locale date as string
let str = OGX.dateToLocale('2021-02-28', {options});
//"Sunday, February 28, 2021"
Options by default are
{ weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
getBestSize
Computes the most appropriate size given a screen dimension and a
size expression
. For more information about Size Expressions, visit the dedicated page.
let size = OGX.Data.getBestSize(1100, '100px|300|500|70%|100%+');
//70%
let size = OGX.Data.getBestSize(1100, '100px|300|500|70%|100%-');
//100
Note that
size expressions
are supported byWindow
andPopup
same
Compares two objects for complete similarities and returns true or false. Set the last parameter to
true
if you want to deep compare all properties
OGX.Data.same({whatever:true}, {whatever:100}, false); //false
eval
Sets or returns the value of the property of an object, when the property is a path to a property
OGX.Data.eval({id:'123', geo:{zip:'A1A 1A1'}}, 'geo.zip'); //returns 'A1A 1A1'
OGX.Data.eval({id:'123', geo:{zip:'A1A 1A1'}}, 'geo.zip', 'Z9Z 9Z9'); //sets geo.zip to 'Z9Z 9Z9'
clipboard
Copy to clipboard given string
OGX.Data.clipboard('my copied text');