Skip to content

Commit 2f47294

Browse files
committed
0.0.1 - add initial methods
1 parent 2fa9945 commit 2f47294

16 files changed

+349
-0
lines changed

.codeclimate.yml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# This is a sample .codeclimate.yml configured for Engine analysis on Code
2+
# Climate Platform. For an overview of the Code Climate Platform, see here:
3+
# http://docs.codeclimate.com/article/300-the-codeclimate-platform
4+
5+
# Under the engines key, you can configure which engines will analyze your repo.
6+
# Each key is an engine name. For each value, you need to specify enabled: true
7+
# to enable the engine as well as any other engines-specific configuration.
8+
9+
# For more details, see here:
10+
# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
11+
12+
# For a list of all available engines, see here:
13+
# http://docs.codeclimate.com/article/296-engines-available-engines
14+
15+
engines:
16+
# to turn on an engine, add it here and set enabled to `true`
17+
# to turn off an engine, set enabled to `false` or remove it
18+
rubocop:
19+
enabled: true
20+
golint:
21+
enabled: true
22+
gofmt:
23+
enabled: true
24+
eslint:
25+
enabled: true
26+
csslint:
27+
enabled: true
28+
29+
# Engines can analyze files and report issues on them, but you can separately
30+
# decide which files will receive ratings based on those issues. This is
31+
# specified by path patterns under the ratings key.
32+
33+
# For more details see here:
34+
# http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
35+
36+
# Note: If the ratings key is not specified, this will result in a 0.0 GPA on your dashboard.
37+
38+
# ratings:
39+
# paths:
40+
# - app/**
41+
# - lib/**
42+
# - "**.rb"
43+
# - "**.go"
44+
45+
# You can globally exclude files from being analyzed by any engine using the
46+
# exclude_paths key.
47+
48+
#exclude_paths:
49+
#- spec/**/*
50+
#- vendor/**/*

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
coverage
3+
.idea
4+
.DS_Store

.idea/jsLibraryMappings.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.jshintrc

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"nonew" : true,
3+
"plusplus" : true,
4+
"curly" : true,
5+
"latedef" : "nofunc",
6+
"unused" : "strict",
7+
"noarg" : true,
8+
"indent" : 2,
9+
"forin" : true,
10+
"noempty" : true,
11+
"quotmark" : "single",
12+
"maxparams" : 5,
13+
"node" : true,
14+
"eqeqeq" : true,
15+
"strict" : true,
16+
"undef" : true,
17+
"bitwise" : true,
18+
"newcap" : true,
19+
"immed" : true,
20+
"browser" : true,
21+
"camelcase" : true,
22+
"nonbsp" : true,
23+
"globals" : {
24+
"after" : false,
25+
"afterEach" : false,
26+
"angular" : false,
27+
"before" : false,
28+
"beforeEach" : false,
29+
"browser" : false,
30+
"describe" : false,
31+
"expect" : false,
32+
"inject" : false,
33+
"it" : false,
34+
"jasmine" : false,
35+
"spyOn" : false,
36+
"$" : false,
37+
"_" : false
38+
}
39+
}

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "0.12"
4+
after_script:
5+
- "./node_modules/coveralls/bin/coveralls.js < ./coverage/lcov.info"

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# alien-node-list-utils
2+
Helper functions for arrays on NodeJS. The functions are pure and curried with Ramda.
3+
4+
[![Build Status](https://travis-ci.org/AlienCreations/alien-node-list-utils.svg?branch=master)](https://travis-ci.org/AlienCreations/alien-node-list-utils) [![Coverage Status](https://coveralls.io/repos/AlienCreations/alien-node-list-utils/badge.svg?branch=master&service=github)](https://coveralls.io/github/AlienCreations/alien-node-list-utils?branch=master) [![npm version](http://img.shields.io/npm/v/alien-node-list-utils.svg)](https://npmjs.org/package/alien-node-list-utils) [![Dependency Status](https://david-dm.org/AlienCreations/alien-node-list-utils.svg)](https://david-dm.org/AlienCreations/alien-node-list-utils)
5+
6+
## Install
7+
8+
```
9+
$ npm install alien-node-list-utils --save
10+
```
11+
12+
Run the specs
13+
14+
```
15+
$ npm test
16+
```
17+
18+
## Methods
19+
20+
#### filterOutItem
21+
Given a list and an item, returns a copy of the list absent said item.
22+
23+
```js
24+
25+
var listUtils = require('alien-node-list-utils'),
26+
list = ['a', 'b', 'c', 'd'];
27+
28+
listUtils.filterOutItem('b', list); // ['a', 'c', 'd']
29+
30+
```
31+
32+
#### filterOutObject
33+
Given a key, value, and list, returns a copy of the list absent the object(s) which match said k/v pair.
34+
35+
```js
36+
37+
var listUtils = require('alien-node-list-utils'),
38+
list = [{ foo : 'bar'}, {baz : 'bat'}, {buz : 'but'}];
39+
40+
listUtils.filterOutObject('baz', 'bat', list); // [{ foo : 'bar'}, {buz : 'but'}]
41+
42+
```
43+
44+
#### maybeDropLastItem
45+
Given a qualifying drop length and a list, returns a new list who's last item is dropped if the list
46+
length exceeds or equals provided drop length
47+
48+
```js
49+
50+
var listUtils = require('alien-node-list-utils'),
51+
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
52+
list2 = ['x', 'y', 'z'];
53+
54+
listUtils.maybeDropLastItem(5, list1); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
55+
listUtils.maybeDropLastItem(5, list2); // ['x', 'y', 'z']
56+
57+
```
58+
59+
#### sortAsc
60+
Simple ascending sorter function
61+
62+
```js
63+
64+
var listUtils = require('alien-node-list-utils'),
65+
list = ['b', 'd', 'a', 'c'];
66+
67+
listUtils.sortAsc(list1); // ['a', 'b', 'c', 'd']
68+
69+
```
70+
71+
#### sortDesc
72+
Simple descending sorter function
73+
74+
```js
75+
76+
var listUtils = require('alien-node-list-utils'),
77+
list = ['b', 'd', 'a', 'c'];
78+
79+
listUtils.sortDesc(list1); // ['d', 'c', 'b', 'a']
80+
81+
```

lib/Lists.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
module.exports = {
4+
maybeDropLastItem : require('./methods/maybeDropLastItem'),
5+
filterOutObject : require('./methods/filterOutObject'),
6+
filterOutItem : require('./methods/filterOutItem'),
7+
sortAsc : require('./methods/sortAsc'),
8+
sortDesc : require('./methods/sortDesc')
9+
};

lib/methods/filterOutItem.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
5+
var filterOutItem = R.curry(function(v, items) {
6+
return R.filter(R.compose(R.not, R.identical(v)), items);
7+
});
8+
9+
module.exports = filterOutItem;

lib/methods/filterOutObject.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
5+
var filterOutObject = R.curry(function(k, v, items) {
6+
return R.filter(R.compose(R.not, R.identical(v), R.prop(k)), items);
7+
});
8+
9+
module.exports = filterOutObject;

lib/methods/maybeDropLastItem.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
5+
var maybeDropLastItem = R.curry(function(dropIfGte, items) {
6+
return R.ifElse(R.compose(R.flip(R.gte)(dropIfGte), R.length), R.dropLast(1), R.clone)(items);
7+
});
8+
9+
module.exports = maybeDropLastItem;

lib/methods/sortAsc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var sortAsc = function(a, b) {
4+
return a < b ? -1 : 1;
5+
};
6+
7+
module.exports = sortAsc;

lib/methods/sortDesc.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
var sortDesc = function(a, b) {
4+
return a > b ? -1 : 1;
5+
};
6+
7+
module.exports = sortDesc;

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name" : "alien-node-list-utils",
3+
"version" : "0.0.1",
4+
"description" : "Helper functions for arrays on NodeJS",
5+
"main" : "lib/Lists.js",
6+
"dependencies" : {
7+
"ramda" : "^0.20.x"
8+
},
9+
"devDependencies" : {
10+
"coveralls" : "^2.11.2",
11+
"istanbul" : "^0.3.13",
12+
"jasmine" : "^2.3.1"
13+
},
14+
"scripts" : {
15+
"test" : "./node_modules/.bin/istanbul cover ./node_modules/.bin/jasmine"
16+
},
17+
"repository" : {
18+
"type" : "git",
19+
"url" : "https://github.com/aliencreations/alien-node-list-utils.git"
20+
},
21+
"keywords" : [
22+
"aliencreations",
23+
"list",
24+
"array",
25+
"arrays",
26+
"util",
27+
"node",
28+
"ramda"
29+
],
30+
"author" : "Sean Cannon",
31+
"license" : "MIT",
32+
"bugs" : {
33+
"url" : "https://github.com/aliencreations/alien-node-list-utils/issues"
34+
},
35+
"homepage" : "https://github.com/aliencreations/alien-node-list-utils"
36+
}

spec/listsSpec.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
var R = require('ramda');
4+
5+
var listUtils = require('../lib/Lists');
6+
7+
var DROP_IF_GTE_LENGTH = 5,
8+
9+
LIST_LONGER = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'],
10+
LIST_LONGER_EXPECT = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'],
11+
12+
LIST_SHORTER = ['x', 'y', 'z'],
13+
LIST_SHORTER_EXPECT = ['x', 'y', 'z'],
14+
15+
LIST_MIXED_UNSORTED = ['b', 'd', 'a', 'c'],
16+
LIST_MIXED_SORTED_ASC = ['a', 'b', 'c', 'd'],
17+
LIST_MIXED_SORTED_DESC = ['d', 'c', 'b', 'a'],
18+
19+
LIST_OF_ITEMS_PRE_FILTERED = ['a', 'b', 'c'],
20+
LIST_OF_ITEMS_FILTERED = ['a', 'c'],
21+
22+
LIST_OF_OBJECTS_PRE_FILTERED = [{ foo : 'bar'}, {baz : 'bat'}, {buz : 'but'}],
23+
LIST_OF_OBJECTS_FILTERED = [{ foo : 'bar'}, {buz : 'but'}],
24+
25+
LIST_DROP_LENGTH = ['q', 'w', 'e', 'r', 't', 'y'],
26+
LIST_DROP_LENGTH_EXPECT = ['q', 'w', 'e', 'r', 't'];
27+
28+
describe('maybeDropLastItem', function() {
29+
it('drops the last item from a list if list length exceeds minimum drop length', function() {
30+
expect(listUtils.maybeDropLastItem(DROP_IF_GTE_LENGTH, LIST_LONGER)).toEqual(LIST_LONGER_EXPECT);
31+
});
32+
it('preserves list if length is less than minimum drop length', function() {
33+
expect(listUtils.maybeDropLastItem(DROP_IF_GTE_LENGTH, LIST_SHORTER)).toEqual(LIST_SHORTER_EXPECT);
34+
});
35+
it('drops the last item from a list if list length is exactly the minimum drop length', function() {
36+
expect(listUtils.maybeDropLastItem(DROP_IF_GTE_LENGTH, LIST_DROP_LENGTH)).toEqual(LIST_DROP_LENGTH_EXPECT);
37+
});
38+
});
39+
40+
describe('filterOutObject', function() {
41+
it('removes an object from a list', function() {
42+
expect(listUtils.filterOutObject('baz', 'bat', LIST_OF_OBJECTS_PRE_FILTERED)).toEqual(LIST_OF_OBJECTS_FILTERED);
43+
});
44+
});
45+
46+
describe('filterOutItem', function() {
47+
it('removes an item from a list', function() {
48+
expect(listUtils.filterOutItem('b', LIST_OF_ITEMS_PRE_FILTERED)).toEqual(LIST_OF_ITEMS_FILTERED);
49+
});
50+
});
51+
52+
describe('sortAsc', function() {
53+
it('sorts a list ascending', function() {
54+
expect(R.sort(listUtils.sortAsc, LIST_MIXED_UNSORTED)).toEqual(LIST_MIXED_SORTED_ASC);
55+
});
56+
});
57+
58+
describe('sortDesc', function() {
59+
it('sorts a list descending', function() {
60+
expect(R.sort(listUtils.sortDesc, LIST_MIXED_UNSORTED)).toEqual(LIST_MIXED_SORTED_DESC);
61+
});
62+
});

spec/support/jasmine.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"spec_dir": "spec",
3+
"spec_files": [
4+
"**/*[sS]pec.js"
5+
],
6+
"helpers": [
7+
"helpers/**/*.js"
8+
]
9+
}

0 commit comments

Comments
 (0)