npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

array-cop

v1.0.0

Published

An [array] utility tool that makes life easier and also does some maths for you.

Downloads

21

Readme

Serve and Array
#Array Cop
npm versionBuild Status Codacy Badge

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:

  • Flatten nested arrays
  • Remove all the duplicates
  • Pick any random item in any range
  • Calculate the sum of all the Number items
  • Calculate an Arithmetic mean
  • Calculate a Geometric mean
  • Calculate a Harmonic mean
  • Find a median
  • Convert to the frequency matrix object
  • Log an array breakdown
  • Remove all the empty items from the nested arrays
  • Filter items in an array by type
  • Convert an object into an array
  • Get rid of all non-alphanumeric characters
  • Get an array of indexes of an element

###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 undefineds 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 ]