Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
An [array] utility tool that makes life easier and also does some maths for you.
Array Cop is a dependency-free vanilla JS nano-library / npm module that deals with arrays. It is just an utility tool that makes life easier and also does some maths for you.
##Current features:
###since version 0.5.0 minified js is not provided anymore. Use normal version in your browser or minify/concat during your usual production routine.
##Usage
###Browser
<script src = "array-cop.js">
###npm
npm install array-cop
var array_ = require('array-cop');
##API
var arr = [8, 1000,["Jack", 8, 'Bob', 'Alice', 5], 1, function x() {return 1 / x}, function a() {}, 2, [2, 3, 4, [5, 4, 6]], 7, 11, 7, [2], {
x: 1,
y: "test"
}];
###I want to...
###...flatten an array:
array_.flatten(arr);
Flattens an array to a single-dimensional one. Goes as deep into subarrays as needed.
array_.flatten(arr);
[ 8, 1000, 'Jack', 8, 'Bob', 'Alice', 5, 1, [Function: x], [Function: a], 2, 2, 3, 4, 5, 4, 6, 7, 11, 7, 2, { x: 1, y: 'test' } ]
###...get rid of the duplicates:
array_.dedup(arr, [force]);
force: Boolean - Optional. Assign to true
if you want to flatten an array and remove duplicates from the sub-arrays as well.
Removes duplicates from an array. Items in nested arrays are not treated as duplicates to avoid mess.
array_.dedup(arr);
[ 8, 1000, [ 'Jack', 8, 'Bob', 'Alice', 5 ], 1, [Function: x], [Function: a], 2, [ 2, 3, 4, [ 5, 4, 6 ] ], 7, 11, [ 2 ], { x: 1, y: 'test' } ]
array_.dedup(arr, true);
[ 1000, 'Jack', 8, 'Bob', 'Alice', 1, [Function: x], [Function: a], 3, 5, 4, 6, 11, 7, 2, { x: 1, y: 'test' } ]
###...get a random array item:
array_.rand(arr,[min],[max]);
min, max: Number — Optional argument, set the range of items to choose from.
Randomly picks and returns one item from an array.
array_.rand(arr,3,8);
[Function: x]
###...get a sum of all items:
array_.sum(arr);
Flattens an array and takes only numeric values into a consideration.
array_.sum(arr);
1075
###...calculate an average:
array_.mean(arr, [type], [precision]);
type: String — Optional, sets the type of mean: 'ari'
: arithmetic, 'geo'
: geometric, 'har'
: harmonic. If omitted then calculates an arithmetic mean.
precision: Number — Optional argument, sets the number of digits after a decimal point. If omitted then falls back to 2.
Flattens an array and takes only numeric values into a consideration.
array_.mean(arr, 'geo', 3);
67.188
array_.mean(arr, 'ari', 2);
5.87
array_.mean(a, 4);
67.1875
###...find a median:
array_.median(arr, [precision]);
precision: Number — Optional argument, sets the number of digits after a decimal point. If omitted then falls back to 2
Flattens an array and takes only numeric values into a consideration.
array_.median(arr, 1);
5.0
###...get a frequency matrix:
array_.freq(arr)
Returns an object item: frequency
, where item
is the value of an each array item and frequency
is the number of times that item appears in an array. Flattens an array before evaluation.
array_.freq(arr);
{ '1': 1,
'2': 3,
'3': 1,
'4': 2,
'5': 2,
'6': 1,
'7': 2,
'8': 2,
'11': 1,
'1000': 1,
Jack: 1,
Bob: 1,
Alice: 1,
'function x() {return 1 / x}': 1,
'function a() {}': 1,
'[object Object]': 1 }
###...get an array breakdown:
array_.breakdown(arr, [toObject]);
toObject: Boolean — Optional argument, if set to true then method will return an object with items sorted by their type.
Service method. Result is an array console pretty print.
array_.breakdown(arr);
Numbers: 16
Strings: 3
Functions: 2
Objects: 1
Undefined: 0
Booleans: 0
Total items: 22
Note that by default you do not need to console.log breakdown()
to have it printed out
array_.breakdown(arr, true);
{ number_: [ 8, 1000, 8, 5, 1, 2, 2, 3, 4, 5, 4, 6, 7, 11, 7, 2 ], string_: [ 'Jack', 'Bob', 'Alice' ], function_: [ [Function: x], [Function: a] ], object_: [ { x: 1, y: 'test' } ], undefined_: [], boolean_: [] }
###...destroy all the empty items in an array:
array_.cop(arr, [toFlatten]);
toFlatten: Boolean — Optional argument. Flattens before all the work.
Goes on patrol and removes all the undefined
s from an array preserving the structure.
array_.cop([1,2,,,,,3,4,[5,,,,,],6,,,,8,3,[[[],9]]]);
[ 1, 2, 3, 4, [ 5 ], 6, 8, 3, [ [ [ ], 9 ] ] ]
array_.cop([1,2,,,,,3,4,[5,,,,,],6,,,,8,3,[[[],9]]], true);
[ 1, 2, 3, 4, 5, 6, 8, 3, 9 ]
###...filter an array by item type or remove some types:
array_.keep(arr, [type], [filter]);
type: String — sets the type of an object to work with. Values are:
'string', 'number', 'function', 'object', 'boolean', 'null', 'undefined'
Default value is 'string'
filter: String — sets the logic for the method.
'all'
— keep all array items of type
, remove the rest
'but'
— keep all array items, but type
Default value is 'all'
array_.keep(arr, 'object', 'all');
[ { x: 1, y: 'test' } ]
array_.keep(arr);
[ 'Jack', 'Bob', 'Alice' ]
array_.keep(arr, 'number', 'but');
[ 'Jack', 'Bob', 'Alice', [Function: x], [Function: a], { x: 1, y: 'test' } ]
###...remove non alphanumerics from the String items:
array_.alpha(arr);
array_.alphaNum(arr);
alpha()
keeps letters only, alphaNum()
saves digits as well.
Method ignores all non-String items to keep them safe. Multidimensional structure is preserved.
array_.alpha(["Clinton 2022", ["__jazzy cat.69", "'s t r\ni n g'"],[["catlady+01"], true]]);
[ 'Clinton', [ 'jazzycat', 'string' ], [ [ 'catlady' ], true ] ]
array_.alphaNum(["Clinton 2022", ["__jazzy cat.69", "'s t r\ni n g'"],[["catlady+01"], true]]);
[ 'Clinton2022', [ 'jazzycat69', 'string' ], [ [ 'catlady01' ], true ] ]
###...convert an object into an array
array_.arrify(object)
Converts an object (associative array) into an array
where object keys are array items.
Argument provided should be an object
. Will throw an error if not an object type.
array_.arrify({name: 'Jack', id: 12345});
['Jack', 12345]
array_.arrify({name: 'Jack', id: 12345, record: [12, 23, 102]});
['Jack', 12345, [12, 23, 102]]
###...get an array of indexes of a given element:
array_.index(arr, element, [preserveStructure])
Flattens an array and returns an array
of index values.
element - mandatory. An element to look for in an array. If element does not exist then -1
is returned.
preserveStructure: Boolean - Optional, if set to true
will not flatten
an array and skip nested arrays.
var arr = ['Humpty','Dumpty', 'Sat', 'On', 'A', null, 'Humpty', 'Dumpty'];
var elem = "Humpty";
var indexList = array_.index(arr, elem);
indexList);
[ 0, 6 ]
array_.index([1, "String", , , 2, 'String', "", [eval('null;'), 2] ], 2 );
[ 2, 6 ] - flattening an array erases
undefined
elements!
array_.index([1, "String", , , 2, 'String', "", [eval('null;'), 2] ], 2, true );
[ 4 ]
FAQs
An [array] utility tool that makes life easier and also does some maths for you.
The npm package array-cop receives a total of 2 weekly downloads. As such, array-cop popularity was classified as not popular.
We found that array-cop demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.