8.4.81 • Published 12 months ago

@zitterorg/sit-vel-delectus v8.4.81

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

@zitterorg/sit-vel-delectus

A toolkit to help solve challenging tasks with object and arrays. Helprjs is fully documented with live Demos and its really simple to use!

Q&A:

Whilst @zitterorg/sit-vel-delectus is fully tested should you ecounter issues please report them.

Most of the examples here make use of this people object.

const people = [
    { id: 1, firstName: 'John', secondName: 'Smith', age: '61', status: 1 },
    { id: 2, firstName: 'John', secondName: 'West', age: '55', status: true },
    { id: 3, firstName: 'Brenda', secondName: 'Holt', age: '60', status: false },
    { id: 4, firstName: 'Sally', secondName: 'Brampton', age: '33', status: undefined },
    { id: 5, firstName: 'June', secondName: 'Chester', age: '47', status: NaN },
    { id: 6, firstName: 'Jack', secondName: 'Carter', age: '24', status: null },
    { id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
    { id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
    { id: 7, firstName: 'Jack', secondName: 'Foster', age: '58', status: 0 },
];

Methods

Methods can be grouped into the following categories:

General : object array manipulation

Array: array only

Boolean : returns true or false

Helpers : deals with values

Numerical : uses numerical data

Vailidation : returns only objects that meet the valid criteria

General

mergeArraysRemoveDuplicates - Demo

merges two arrays and removes all duplicates
Useage:

const array1 = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
const array2 = [{ id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }];

const mergedArray = mergeArraysRemoveDuplicates(array1, array2);

deGroup - Demo

reverses a grouped object.
deGroup(people, groupObject)
returns 9 objects

objectToArray - Demo

pass in an object
Useage: objectToArray({firstName: 'Mike', lastName: 'Jones', age: 34, registered: true})
Returns ['Mike', 'Jones', 34, true]

arrayToObject - Demo

pass in an array of values
Useage: arrayToObject(["Mike","Jones",34,true])
Returns {0: 'Mike', 1: 'Jones', 2: 34, 3: true}

pass in an array of values and custom keys
Useage: arrayToObject(["Mike","Jones",34,true], ["firstName", "lastName","age","registered"])
Returns {firstName: 'Mike', lastName: 'Jones', age: 34, registered: true}

addNewProperty - Demo

addNewProperty(people, 'registered', true)
adds new key/value to all objects.

countKeys - Demo

countKeys({a:1, b:2, c:3});
// 3

CSVtoArray - Demo

CSVtoArray(csvfile');
// [ {...} {...} ]

getEven - Demo

getEven(people,'age');
returns objects containing only even values

getObject - Demo

getObject(people,2);
returns an object from an array from array key

getOdd - Demo

getOdd(people,'age');
returns objects containing only odd values

getValues - Demo

getValues(people, 'firstName');
// ["John", "John", "Brenda", "Sally", "June", "Jack", "Jack"]

groupByKey - Demo

groupByKey(people, 'firstName')
// {John: Array(2), Brenda: Array(1), Sally: Array(1), June: Array(1), Jack: Array(2)}

mergeArrays - Demo

mergeArrays(arr1,arr2,key); 
returns a single merged array

randomOrder - Demo

randomOrder(people);
returns randomly ordered array

refine - Demo

const result = refine(people, "firstName", "Jack");
// return only objects that match criteria

removeDuplicates - Demo

removeDuplicates(people, 'firstName');
removes all objects containing duplicates values

removeFalsy - Demo

removeFalsy(people, 'status');
removes all objects containing falsy values

removeValue - Demo

removeValue(people,'firstName', 'John');
remove all objects that have this value
Note: 1 and '1' will give a different result, be sure to include quotation marks if targetting a string

setAllValues - Demo

setAllValues(people, 'age', '25');
(sets all values to 25)

sortByKey - Demo

sortByKey(people, 'age', 'desc');
returns object in value order
Note, 3rd paramenter is optional.  desc = decending order.

toArray - Demo

toArray(people, 'age');
// ["61","55","60","33","47","24","58"]

toLower - Demo

toLower(people, 'firstName');
returns object with values as lowercase

toUpper - Demo

toUpper(people, 'firstName');
returns object with values as uppercase

toString - Demo

toString(people, 'id');
returns object with values as a string

toTruncate - Demo

toTruncate(people, 'firstName', 3);
returns object with values truncated to numerical value

toNumber - Demo

toNumber(people, 'age');
returns object with values as numbers

toTrim - Demo

toTrim(people, 'firstName');
returns object values with whitespace removed

Array

isEmptyArray

isEmptyArray([1,2]);
false

getAverage

getAverage([1, 2, 3, 4, 4, 4]));
3

concatArray - Demo

pass in an array of arrays
Useage: concatArray([ [1,2],["three", "four"],[5],[6] ])
Returns [1,2,"three","four",5,6]

populateArray - Demo

populateArray(0,20,4)
// [4,8,12,16,20]

uniqueArray - Demo

uniqueArray(["one",1,1,2,3,"two","three","four","one","two"])
// ["one", 1, 2, 3, "two", "three", "four"]

emptyArray - Demo

emptyArray(["one","two","three","four"])
// []

reverseArray - Demo

reverseArray([0,1,2,3,4,5,6])
// [6,5,4,3,2,1,0]

shuffleArray - Demo

shuffleArray([0,1,2,3,4,5,6])
// [4,0,1,6,5,3,2]

Boolean

isAll - Demo

pass in object array, key and function. 

const isBelow = (currentValue) => currentValue < 99;
const result = isAll(people, 'age', isBelow)
console.log('result', result)
// true

isPresent - Demo

pass in object array, key and function. 

const age = 48;
const isBelow = (currentValue) => currentValue < age;
const result = isPresent(people, "age", isBelow);
console.log('result', result)
// true

isArray - Demo

isArray([1,2,3]);
// true

isBigint - Demo

isBigint(9007199254740991n);
// true

isBoolean - Demo

isBoolean(true);
// true

isNaN - Demo

isNaN(NaN);
// true

isNull - Demo

isNull(null);
// true

isNumber - Demo

isNumber(1);
// true

isObject - Demo

isObject({x:1, y:2});
// true

isString - Demo

isString('abc'});
// true

isSymbol - Demo

isSymbol(Symbol());
// true

isUndefined - Demo

isUndefined(undefined);
// true

Helpers

percentage - Demo

percentage(partial, total)

// percentage(50, 200)
// 25
// calculate percentage of partial against total number

typeOf - Demo

typeOf(value);

// typeOf(1); returns "number"
// typeOf([1,2,3]); returns "array"
// typeOf({x: 1}); returns "object"

randomId - Demo

randomId();

// Random ID generator
// zxrm95d6ug

String

capitalise('hello world')
Returns 'Hello world'

Numerical

getMaximum - Demo

getMaximum(people,'age');
// 61

getMinimum - Demo

getMinimum(people,'age');
// 24

getTotal - Demo

getTotal(people, 'age');
returns sum total

Validation

getValidEmail

getValidEmail( [
    { id: 1, email: 'badEmailDotgmail.com'  },
    { id: 2, email: 'test@validEmail.com'  },
    { id: 3, email: 'test@badEmail' }
],'email')
returns only objects containing valid email addresses 

getValidString

getValidString(people, 'firstName', 5, 99)
//Note: (array, key, minimumLength, maximumLength)
returns only objects containing text within min and max length

Combination - Demo

In this example five methods have been used to generate a desired output.  
It is overengineered and an unlikely requirement however it demonstrates 
more possibilities when using @zitterorg/sit-vel-delectus.  
class-validatorponyfilloptimizerroutepostcsstapemapreduceefficientpackageskinesisl10nES2023sharedarraybufferhasOwnawsSymbolurlargsobjectrateIteratorgraphqlformpipesortedkarmamomentreact-hook-formYAMLebstrimhookformfast-deep-copyshamcommandcopyprivate datas3schemasuperagentsqsclimulti-packagevalidationpromisesdeepcopydynamodbapibytecensorvpcreadableFunction.prototype.namedom-testing-librarywordwrappicomatchless compilerspecvalueschromeawaitastURLSearchParamsES3io-tsURLwatchingJSON-SchemaUint32ArrayUint8ClampedArrayArray.prototype.filterstyled-componentsfastjsdiffreducerjavascriptconfigurableeslintbannerjwtstylingrangeerroresES7findLastIndexschemeinwafonceutil.inspectArrayBuffer#slicelessforEachconsolegetterconcatlocationbundlingclassnameslockfiletoolkitmergermemojiRFC-6455namesES2022serializeremoveInt8ArrayrecursiveArray.prototype.flattenmixinsreact-testing-libraryworkspace:*performanceargparsepackage managerdeep-copybufferrm -frmonorepoWeakMapextensioncss lesspolyfillemrfromreal-timewordbreakwgetcolorsphonehas-ownObservablesTypeScriptpnpm9package0terminalArray.prototype.flatMapfast-deep-cloneajax[[Prototype]]preserve-symlinkslibphonenumberRegExp#flagsECMAScript 2020browserlistemitpropertiesmkdirptapmiddlewarefluxviewflatMapprefixframeworkshrinkwrapnativedataViewtc39batchnumberless mixinsprunegetoptlengthregular expressionfast-cloneelmmetadatabrowserlimitescapeagentmake dirBigUint64ArraypusharraybufferPromise@@toStringTagfullswfnopewalkenderperformantcommanderdependency managerjapaneseasyncrgbgetintrinsiccss variablejsxxtermes2015qsiterationplugincolorUint8Arraydiffendpointnodejsstylejsonpathduplextyped arrayrequireimportexportpatchenvironmentindicatorESfseventsbdddatetermroutingconnectcollectionexpressiontestergradients cssstartertoArrayclonedirectorylesscssgetOwnPropertyDescriptorcachestdliba11ytrimRightuninstallquerystringwarningStreames2018sequencechineseObservablecomparepostcss-plugincharacternested cssaccessorconcatMapguidparseRegExp.prototype.flagsECMAScript 2022ObjectObject.definePropertymkdirscloudformationpathevents$.extendroute53keysajvfigletcircularlanguagebootstrap lesses2016lazytypeerrorES6ECMAScript 2016stringArrayBuffer.prototype.slicelistenersslotcoerciblestoragegatewayUnderscore
8.4.81

12 months ago

8.4.77

12 months ago

8.4.78

12 months ago

8.4.79

12 months ago

8.4.80

12 months ago

8.4.74

12 months ago

8.4.75

12 months ago

8.4.76

12 months ago

8.4.71

12 months ago

8.4.72

12 months ago

8.4.73

12 months ago

8.4.70

12 months ago

8.4.67

1 year ago

8.4.68

12 months ago

8.4.69

12 months ago

8.3.65

1 year ago

8.3.66

1 year ago

7.3.52

1 year ago

7.3.54

1 year ago

7.3.53

1 year ago

7.3.56

1 year ago

7.3.55

1 year ago

8.3.60

1 year ago

8.3.61

1 year ago

8.3.62

1 year ago

8.3.63

1 year ago

8.3.64

1 year ago

6.2.51

1 year ago

6.2.52

1 year ago

6.2.50

1 year ago

5.2.40

1 year ago

6.2.48

1 year ago

6.2.49

1 year ago

6.2.42

1 year ago

6.2.43

1 year ago

6.3.52

1 year ago

6.2.40

1 year ago

6.2.41

1 year ago

6.2.46

1 year ago

6.2.47

1 year ago

6.2.44

1 year ago

6.2.45

1 year ago

5.2.39

1 year ago

5.2.38

1 year ago

5.2.37

1 year ago

5.2.36

1 year ago

5.2.35

1 year ago

5.2.34

1 year ago

5.2.33

1 year ago

8.4.66

1 year ago

8.3.56

1 year ago

8.3.57

1 year ago

8.3.58

1 year ago

8.3.59

1 year ago

5.2.32

1 year ago

5.2.31

1 year ago

5.2.30

1 year ago

5.2.29

1 year ago

5.2.28

1 year ago

5.1.28

1 year ago

5.1.27

1 year ago

5.1.26

1 year ago

4.0.19

1 year ago

3.0.18

1 year ago

4.1.19

1 year ago

3.0.19

1 year ago

2.0.18

1 year ago

4.1.20

1 year ago

4.1.21

1 year ago

4.1.22

1 year ago

4.1.23

1 year ago

4.1.24

1 year ago

4.1.25

1 year ago

4.1.26

1 year ago

2.0.17

1 year ago

2.0.16

1 year ago

2.0.15

1 year ago

2.0.13

1 year ago

2.0.14

1 year ago

2.0.12

1 year ago

2.0.11

1 year ago

2.0.10

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.2

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago