Skip to content

Commit

Permalink
0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Oct 28, 2014
1 parent 90ed3b0 commit 7bbd10f
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 213 deletions.
11 changes: 7 additions & 4 deletions Makefile
Expand Up @@ -17,16 +17,19 @@ dist_cjs:
dist_amd:
cd src && $(FUME) $(SRC_FILES) -amdify -o ../$(DIST_DIR)/amd && cd ..

dist_umd:
cd src && $(FUME) $(SRC_FILES) -umdify -o ../$(DIST_DIR) && cd ..

rm_dist:
rm -rf $(DIST_DIR)

build: rm_dist dist_amd dist_cjs
build: rm_dist dist_umd

test: build
$(MOCHA) --recursive --reporter spec $(TESTS_DIR)
$(MOCHA) --recursive --reporter spec $(TEST_DIR)

coverage: build
$(ISTANBUL) cover $(_MOCHA) -- --recursive --reporter spec $(TESTS_DIR)
$(ISTANBUL) cover $(_MOCHA) -- --recursive --reporter spec $(TEST_DIR)

travis: install build
$(ISTANBUL) cover --report lcovonly $(_MOCHA) -- --recursive --reporter spec --bail $(TESTS_DIR) && cat ./coverage/lcov.info | $(COVERALLS)
$(ISTANBUL) cover --report lcovonly $(_MOCHA) -- --recursive --reporter spec --bail $(TEST_DIR) && cat ./coverage/lcov.info | $(COVERALLS)
2 changes: 1 addition & 1 deletion README.md
@@ -1 +1 @@
[![Version](http://img.shields.io/npm/v/mu-unique.svg)](https://www.npmjs.org/package/mu-unique)[![Version](http://img.shields.io/bower/v/mu-unique.svg)](https://github.com/mu-lib/mu-unique)[![Build Status](https://api.travis-ci.org/mu-lib/mu-unique.svg?branch=master)](https://travis-ci.org/mu-lib/mu-unique)[![Coverage Status](https://img.shields.io/coveralls/mu-lib/mu-unique/master.svg)](https://coveralls.io/r/mu-lib/mu-unique)# mu-uniqueUniqueify an array of elements with a natural or artificial order.**Runtime complexity:** `O(n*log(n))``unique(arr, order)`0. `arr {Array}` - The source array.0. `order {Function}` - A custom order function. Optional. If a function is specified, it is called with 2 elements from the array and should return `0` if the are equal, a positive number if `a` is bigger and a negative number if `b` is bigger. **Notes:**0. The function modifies the array and returns its new length.0. The original order of items is not preserved.0. If the `this` value of the function is defined, it will be used as the array (see examples). Thus it is possible to use this function to extend the Array prototype: `Array.prototype.unique = unique`.## Installation- Node: 0. `npm install mu-unique` 0. `var unique = require('mu-unique');`- AMD (install with bower): 0. `bower install mu-unique` 0. `require(['mu-unique'], function(unique){ /* ... */ });` Build AMD and CJS dists with `make build`. Run tests with `make test`.Run coverage analysis with `make coverage` (coverage report is saved to `./coverage`).## Examples```Javascriptvar arr = [1, 2, 1, 2, 3], len = unique(arr);console.log(arr); // [1, 2, 3]console.log(len)l // 3```**With a custom comparator:**```Javascriptvar arr1 = ['hello', 'hi', 'world'], arr2 = unique(arr1, true, function(a, b){ // compare only first character return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0; });console.log(arr1); // ['hello', 'hi', 'world']console.log(arr2); // ['hello', 'world']```**By setting the `this` value:**```Javascriptvar arr = [1, 2, 1, 2, 3], len = unique.call(arr);console.log(arr); // [1, 2, 3]console.log(len)l // 3```
[![Version](http://img.shields.io/npm/v/mu-unique.svg)](https://www.npmjs.org/package/mu-unique)[![Version](http://img.shields.io/bower/v/mu-unique.svg)](https://github.com/mu-lib/mu-unique)[![Build Status](https://api.travis-ci.org/mu-lib/mu-unique.svg?branch=master)](https://travis-ci.org/mu-lib/mu-unique)[![Coverage Status](https://img.shields.io/coveralls/mu-lib/mu-unique/master.svg)](https://coveralls.io/r/mu-lib/mu-unique)# mu-uniqueUniqueify an array of elements.**Runtime complexity:** `O(n*log(n))` for sortable arrays, `O(n^2)` fornon-sortable arrays.`unique(arr, sortable, comparator)`0. `arr {Array}` - The source array.0. `sortable {Boolean}` - Optional. Does the array contain sortable elements? **defaults to `false`**.0. `comparator {Function}` - Optional. - If the array is sortable, it defines the order of the elements in the array. It is called with 2 elements from the array and should return `0` if they are equal, a positive number if `a` is bigger and a negative number if `b` is bigger. If not provided, the array will be sorted with a natural order (as defined by the runtime). - If the array is not sortable, it defines the equality between elements in the array. It is called with 2 elements from the array and should return `true` if they are equal, and `false` otherwise. If not provided, the elements will be tested for equality with `===`. **Notes:**0. The function modifies the array and returns its new length.0. The original order of items may not be preserved.0. If the `this` value of the function is defined, it will be used as the array (see examples). Thus it is possible to use this function to extend the Array prototype: `Array.prototype.unique = unique`.## Installation- Node: 0. `npm install mu-unique` 0. `var unique = require('mu-unique');`- AMD (install with bower): 0. `bower install mu-unique` 0. `require(['mu-unique'], function(unique){ /* ... */ });` Build AMD and CJS dists with `make build`. Run tests with `make test`.Run coverage analysis with `make coverage` (coverage report is saved to `./coverage`).## Examples```Javascriptvar arr = [1, 2, 1, 2, 3], len = unique(arr);console.log(arr, true); // [1, 2, 3]console.log(len); // 3``````Javascriptvar o1 = {}, o2 = function(){}, o3 = "foo", arr = [o1,o1,o2,o3,o2,o3], len = unique(arr);console.log(arr); // [o1, o2, o3]console.log(len); // 3```**With a custom order:**```Javascriptvar arr1 = ['hello', 'hi', 'world'], arr2 = unique(arr1, function(a, b){ // compare only first character return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0; });console.log(arr1); // ['hello', 'hi', 'world']console.log(arr2); // ['hello', 'world']```**By setting the `this` value:**```Javascriptvar arr = [1, 2, 1, 2, 3], len = unique.call(arr);console.log(arr); // [1, 2, 3]console.log(len); // 3```
Expand Down
6 changes: 4 additions & 2 deletions bower.json
@@ -1,12 +1,12 @@
{
"name": "mu-unique",
"version": "0.0.2",
"version": "0.0.3",
"homepage": "https://github.com/mu-lib/mu-unique",
"authors": [
"µLib Team (https://github.com/mu-lib)"
],
"description": "Uniqueify an array",
"main": "./unique.js",
"main": "./dist/unique.js",
"moduleType": [
"amd",
"node"
Expand All @@ -20,6 +20,8 @@
"license": "MIT",
"ignore": [
"**/.*",
"makefile",
"src",
"node_modules",
"bower_components",
"test",
Expand Down
5 changes: 0 additions & 5 deletions dist/amd/sorter.js

This file was deleted.

33 changes: 0 additions & 33 deletions dist/amd/unique.js

This file was deleted.

5 changes: 0 additions & 5 deletions dist/cjs/sorter.js

This file was deleted.

33 changes: 0 additions & 33 deletions dist/cjs/unique.js

This file was deleted.

13 changes: 13 additions & 0 deletions dist/sorter.js
@@ -0,0 +1,13 @@
(function () {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else
throw Error('Cannot find a module loader');
function factory() {
return function (arr, order) {
Array.prototype.sort.call(arr, order);
};
}
}());
83 changes: 83 additions & 0 deletions dist/unique.js
@@ -0,0 +1,83 @@
(function () {
if (typeof define === 'function' && define.amd) {
define(['./sorter'], uniqueFactory);
} else if (typeof exports === 'object') {
module.exports = uniqueFactory(require('./sorter'));
} else
throw Error('Cannot find a module loader');
function uniqueFactory(sorter) {
var undefined, DEFAULT_SORTABLE = false;
function trivialOrder(a, b) {
return a > b ? 1 : a < b ? -1 : 0;
}
function trivialCompare(a, b) {
return a === b;
}
function _uniqueifySorted(arr, order) {
var i = 0, n = 1, len = arr.length;
if (len < 2)
return;
while (i < len) {
if (order(arr[i], arr[n - 1]) !== 0) {
arr[n] = arr[i];
n++;
}
i++;
}
arr.length = n;
}
function _uniqueifyNotSorted(arr, compare) {
var i = 0, j, len = arr.length;
while (i < len) {
j = i + 1;
while (j < len) {
if (compare(arr[i], arr[j])) {
arr[j--] = arr[--len];
}
j++;
}
i++;
}
arr.length = len;
}
return function () {
var arr, sortable, comparator, args = Array.prototype.slice.call(arguments, 0);
if (args.length === 0)
args.push(this);
if (args.length === 1) {
if (Object.prototype.toString.call(args[0]) !== '[object Array]')
args.unshift(this);
else
args.push(DEFAULT_SORTABLE);
}
if (args.length === 2) {
if (Object.prototype.toString.call(args[0]) !== '[object Array]') {
args.unshift(this);
} else if (args[1] === true) {
args.push(trivialOrder);
} else if (args[1] === false) {
args.push(trivialCompare);
} else {
args[2] = args[1];
args[1] = DEFAULT_SORTABLE;
}
}
arr = args[0];
sortable = args[1];
comparator = args[2];
if (Object.prototype.toString.call(arr) !== '[object Array]')
throw Error('\'arr\' must be an array');
if (!(sortable === true || sortable === false))
throw Error('\'sortable\' must be a boolean');
if (!(typeof comparator === 'function'))
throw Error('\'comparator\' must be a function');
if (sortable) {
sorter(arr, comparator);
_uniqueifySorted(arr, comparator);
} else {
_uniqueifyNotSorted(arr, comparator);
}
return arr.length;
};
}
}());
60 changes: 30 additions & 30 deletions package.json
@@ -1,32 +1,32 @@
{
"name": "mu-unique",
"version": "0.0.2",
"description": "Uniqueify an array",
"main": "unique.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/mu-lib/mu-unique.git"
},
"keywords": [
"unique",
"array",
"in-place",
"comparator"
],
"author": "µLib Team (https://github.com/mu-lib)",
"license": "MIT",
"bugs": {
"url": "https://github.com/mu-lib/mu-unique/issues"
},
"homepage": "https://github.com/mu-lib/mu-unique",
"devDependencies": {
"coveralls": "^2.11.2",
"fume": "0.0.0",
"istanbul": "^0.3.2",
"mocha": "^1.21.5",
"should": "^4.1.0"
}
"name": "mu-unique",
"version": "0.0.3",
"description": "Uniqueify an array",
"main": "dist/unique.js",
"scripts": {
"test": "make test"
},
"repository": {
"type": "git",
"url": "https://github.com/mu-lib/mu-unique.git"
},
"keywords": [
"unique",
"array",
"in-place",
"comparator"
],
"author": "µLib Team (https://github.com/mu-lib)",
"license": "MIT",
"bugs": {
"url": "https://github.com/mu-lib/mu-unique/issues"
},
"homepage": "https://github.com/mu-lib/mu-unique",
"devDependencies": {
"coveralls": "^2.11.2",
"fume": "0.0.1",
"istanbul": "^0.3.2",
"mocha": "^1.21.5",
"should": "^4.1.0"
}
}
2 changes: 1 addition & 1 deletion src/unique.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7bbd10f

Please sign in to comment.