Skip to content

Commit

Permalink
Adding arrify method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbykoff committed Jun 15, 2016
1 parent 3a58460 commit d6464e6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 66 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Array Cop
[![GitHub version](https://badge.fury.io/gh/tomkallen%2Farray-cop.svg)](https://badge.fury.io/gh/tomkallen%2Farray-cop) [![npm version](https://badge.fury.io/js/array-cop.svg)](https://badge.fury.io/js/array-cop) [![Build Status](https://travis-ci.org/tomkallen/array-cop.svg?branch=master)](https://travis-ci.org/tomkallen/array-cop)

:cop: Arraycop is a dependency-free vanilla JS nano-library / npm module that deals with arrays.
:cop: 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. :police_car:

##Current features:
Expand All @@ -19,12 +19,15 @@ It is just an utility tool that makes life easier and also does some maths for y
- Remove all the empty items from the nested arrays
- Filter items in an array by type
- Get rid of all non-alphanumeric characters **(0.4.0)+**
- Convert an object into an array **(0.5.0)+**

##since version 0.5.0 minified js is not provided anymore. Use normal version in your browser and minify/concat during your usual production routine.

----------

##Usage
###Browser
`<script src = "array-cop.min.js">`
`<script src = "array-cop.js">`
###npm
`npm install array-cop`

Expand Down Expand Up @@ -184,3 +187,13 @@ Method ignores all non-String items to keep them safe. Multidimensional structur
>[ 'Clinton2022',
[ 'jazzycat69', 'string' ],
[ [ 'catlady01' ], true ] ]

###**...convert an object into an array**
**`array_.arrify(object)`**
Coverts 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.
`console.log(array_.arrify({name: 'Jack', id: 12345}));`
> ['Jack', 12345]
`console.log(array_.arrify({name: 'Jack', id: 12345, record: [12, 23, 102]}));`
> ['Jack', 12345, [12, 23, 102]]
59 changes: 30 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
{
"name": "array-cop",
"version": "0.4.2",
"description": "An [array] utility tool that makes life easier and also does some maths for you.",
"main": "src/array-cop.js",
"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tomkallen/array-cop.git"
},
"keywords": [
"arrays",
"array",
"utils",
"utility",
"maths"
],
"author": "Alex Bykoff <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomkallen/array-cop/issues"
},
"homepage": "https://github.com/tomkallen/array-cop#readme",
"devDependencies": {
"chai": "3.5.0",
"istanbul": "0.4.3",
"mocha": "2.5.3"
}
"name": "array-cop",
"version": "0.5.0",
"description": "An [array] utility tool that makes life easier and also does some maths for you.",
"main": "src/array-cop.js",
"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tomkallen/array-cop.git"
},
"keywords": [
"arrays",
"array",
"utils",
"utility",
"maths",
"flatten"
],
"author": "Alex Bykoff <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomkallen/array-cop/issues"
},
"homepage": "https://github.com/tomkallen/array-cop#readme",
"devDependencies": {
"chai": "3.5.0",
"istanbul": "0.4.3",
"mocha": "2.5.3"
}
}
11 changes: 11 additions & 0 deletions src/array-cop.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,17 @@
}
});
return result;
},

arrify: function(obj) {

if (typeof obj === 'object') {

return Object.keys(obj).map(function(key) {
return obj[key];
});
}
throw new Error("Not an object!");
}
}

Expand Down
1 change: 0 additions & 1 deletion src/array-cop.min.js

This file was deleted.

95 changes: 62 additions & 33 deletions test/array-cop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ var assert = chai.assert;
var expect = chai.expect;

var arr = [1, 3, [3, [5, ]], 7, 8, 'pete', {}];
var srcArray = [1, 3, 3, 'Some string', 'Some string', 5, 7, 8, 'pete', {}];

describe('flatten', function() {
// FLATTEN
describe('flatten() - Flattens an array', function() {

it('should return a flattened array', function() {

Expand All @@ -18,24 +20,28 @@ describe('flatten', function() {

assert.deepEqual(arrayCop.flatten("not an array"), "not an array");
});

});

var srcArray = [1, 3, 3, 'Some string', 'Some string', 5, 7, 8, 'pete', {}];
describe('dedup', function() {
// DEDUP
describe('dedup() - Removes duplicates', function() {

it('should removes duplicates from an array', function() {
assert.deepEqual(arrayCop.dedup(srcArray), [1, 3, 'Some string', 5, 7, 8, 'pete', {}]);
});
});

describe('rand', function() {
})

// RAND
describe('rand() - Returns a random item', function() {

it('should return 2nd element', function() {
assert.deepEqual(arrayCop.rand(srcArray, 1, 2), 3);
});
});

describe('sum', function() {
// SUM
describe('sum() - Returns a sum of all Number items', function() {

it('should return sum of all element with type number', function() {
assert.deepEqual(arrayCop.sum(srcArray), 27);
Expand All @@ -53,7 +59,8 @@ describe('sum', function() {
});
});

describe('cop', function() {
// COP
describe('cop() - Removes all the empty items', function() {

it('should throw an error if argument is not an array', function() {
expect(function() {
Expand Down Expand Up @@ -82,40 +89,62 @@ describe('cop', function() {
});
});

describe('mean', function() {
// MEAN
describe('mean() - Calculate and return Mean values', function() {

it('should throw an error if argument is not an array', function() {
expect(function() {
arrayCop.mean(new String)
it('should throw an error if argument is not an array', function() {
expect(function() {
arrayCop.mean(new String)
}).to.throw('Not an array!');
});
});

it('should return 0 for empty array', function() {
assert.deepEqual(arrayCop.mean([]), 0);
});
it('should return 0 for empty array', function() {
assert.deepEqual(arrayCop.mean([]), 0);
});

it('should return ariphmetic mean for array by default', function() {
assert.deepEqual(arrayCop.mean([1,2,3]), '2.00');
});
it('should return ariphmetic mean for array by default', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3]), '2.00');
});

it('should return ariphmetic mean for array', function() {
assert.deepEqual(arrayCop.mean([1,2,3, new String, [1,2,3]], 'ari'), '2.00');
});
it('should return ariphmetic mean for array', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3, new String, [1, 2, 3]], 'ari'), '2.00');
});

it('should return ariphmetic mean for array with .000 precision', function() {
assert.deepEqual(arrayCop.mean([1,2,3], 3), '2.000');
});
it('should return ariphmetic mean for array with .000 precision', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3], 3), '2.000');
});

it('should return geometric mean for flatterned array', function() {
assert.deepEqual(arrayCop.mean([1,2,3, new String, new Object, [1,2,3]],'geo'), '1.82');
});
it('should return geometric mean for flatterned array', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3, new String, new Object, [1, 2, 3]], 'geo'), '1.82');
});

it('should return geometric mean for flatterned array with .00000 precision', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3, new String, new Object, [1, 2, 3]], 'geo', 5), '1.81712');
});

it('should return harmonic mean for array with .000 precision', function() {
assert.deepEqual(arrayCop.mean([1, 2, 3], 'har', 3), '1.636');
});

});

// ARRIFY
describe('arrify - Converts object to an array that consists of values of the given object', function() {

it('should return geometric mean for flatterned array with .00000 precision', function() {
assert.deepEqual(arrayCop.mean([1,2,3, new String, new Object, [1,2,3]],'geo', 5), '1.81712');
});
it('should convert to an array and return it', function() {
assert.deepEqual(arrayCop.arrify({
user: 'Jack',
id: 17633
}), ['Jack', 17633]);
});

it('should return harmonic mean for array with .000 precision', function() {
assert.deepEqual(arrayCop.mean([1,2,3], 'har', 3), '1.636');
});
it('should throw an error when argument provided is not an object', function(){
expect(function() {
arrayCop.arrify(new Function)
}).to.throw('Not an object!');
});

it('should treat Array as an object', function(){
assert.isArray(arrayCop.arrify(new Array));
});
});
1 change: 0 additions & 1 deletion test/array-cop.test.min.js

This file was deleted.

0 comments on commit d6464e6

Please sign in to comment.