-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [0.2.0](https://github.com/rmdm/nodups/compare/v0.1.3...v0.2.0) - 2017-11-02 | ||
- `polyfill()` added | ||
|
||
## 0.1.3 - 2017-11-01 | ||
Initial release. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./dst/nodups').polyfill() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ const isEqualWith = require('lodash.isequalwith') | |
* @param {Function} [options.onUnique] | ||
*/ | ||
|
||
export default function (array, options = {}) { | ||
export default function nodups (array, options = {}) { | ||
|
||
if (!Array.isArray(array)) { | ||
return [] | ||
|
@@ -37,6 +37,13 @@ export default function (array, options = {}) { | |
return uniques | ||
} | ||
|
||
nodups.polyfill = function () { | ||
Array.prototype.nodups = function (options) { | ||
return nodups(this, options) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
zlumer
|
||
} | ||
return nodups | ||
} | ||
|
||
function getUniques (array, options) { | ||
|
||
const uniques = [] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict' | ||
|
||
import assert from 'assert-match' | ||
import nodups from '../src/nodups' | ||
|
||
describe('nodups polyfill', function () { | ||
|
||
it('is not available until activated', function () { | ||
assert.throws(function () { | ||
[ 1, 1, 1 ].nodups() | ||
}) | ||
}) | ||
|
||
context('when activated', function () { | ||
|
||
before(function () { | ||
nodups.polyfill() | ||
}) | ||
|
||
it('returns nodups module itself', function () { | ||
const polyfilled = nodups.polyfill() | ||
assert.equal(polyfilled, nodups) | ||
}) | ||
|
||
it('makes available nodups convenient method on Array.prototype', function () { | ||
assert.deepEqual([ 1, 1, 1 ].nodups(), [ 1 ]) | ||
}) | ||
|
||
it('added method accepts and applies options', function () { | ||
|
||
const result = [ 15, 5, 14, 33, 73 ].nodups({ | ||
compare: (a,b) => a % 10 === b % 10 | ||
}) | ||
|
||
assert.deepEqual(result, [ 15, 14, 33 ]) | ||
}) | ||
|
||
it('allows for method chaining', function () { | ||
|
||
const array = [ | ||
{ | ||
title: 'ball', | ||
category: 'physical activity toys', | ||
}, | ||
{ | ||
title: 'frisbee', | ||
category: 'physical activity toys', | ||
}, | ||
{ | ||
title: 'ball', | ||
category: 'physical activity toys', | ||
}, | ||
{ | ||
title: 'quicksort', | ||
category: 'algorithms', | ||
}, | ||
{ | ||
title: 'hard coding', | ||
category: 'anti-patterns', | ||
}, | ||
{ | ||
title: 'espresso', | ||
category: 'coffee', | ||
}, | ||
{ | ||
title: 'espresso', | ||
category: 'coffee', | ||
}, | ||
{ | ||
title: 'magic number', | ||
category: 'anti-patterns', | ||
}, | ||
{ | ||
title: 'espresso', | ||
category: 'coffee', | ||
}, | ||
{ | ||
title: 'hard coding', | ||
category: 'anti-patterns', | ||
}, | ||
] | ||
|
||
const categoryTypes = { | ||
'physical activity toys': 'toys', | ||
'algorithms': 'programming', | ||
'anti-patterns': 'programming', | ||
'coffee': 'drinks', | ||
} | ||
|
||
function getType (category) { | ||
return categoryTypes[category] | ||
} | ||
|
||
const result = array | ||
.nodups() | ||
.map(x => x.category) | ||
.nodups() | ||
.filter(x => x[0] === 'a') | ||
.map(getType) | ||
.nodups() | ||
|
||
assert(result, [ 'programming' ]) | ||
}) | ||
}) | ||
}) |
I guess this can be reduced to
Array.prototype.nodups = nodups
Or am I missing something here?