Sha256: 2c4a057466c7e78911c516055352f21a73e08c6983b491cec66fe44c0e0f80d4

Contents?: true

Size: 1.11 KB

Versions: 38

Compression:

Stored size: 1.11 KB

Contents

'use strict';

// there's 3 implementations written in increasing order of efficiency

// 1 - no Set type is defined
function uniqNoSet(arr) {
	var ret = [];

	for (var i = 0; i < arr.length; i++) {
		if (ret.indexOf(arr[i]) === -1) {
			ret.push(arr[i]);
		}
	}

	return ret;
}

// 2 - a simple Set type is defined
function uniqSet(arr) {
	var seen = new Set();
	return arr.filter(function (el) {
		if (!seen.has(el)) {
			seen.add(el);
			return true;
		}

		return false;
	});
}

// 3 - a standard Set type is defined and it has a forEach method
function uniqSetWithForEach(arr) {
	var ret = [];

	(new Set(arr)).forEach(function (el) {
		ret.push(el);
	});

	return ret;
}

// V8 currently has a broken implementation
// https://github.com/joyent/node/issues/8449
function doesForEachActuallyWork() {
	var ret = false;

	(new Set([true])).forEach(function (el) {
		ret = el;
	});

	return ret === true;
}

if ('Set' in global) {
	if (typeof Set.prototype.forEach === 'function' && doesForEachActuallyWork()) {
		module.exports = uniqSetWithForEach;
	} else {
		module.exports = uniqSet;
	}
} else {
	module.exports = uniqNoSet;
}

Version data entries

38 entries across 37 versions & 14 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/array-uniq/index.js
disco_app-0.18.0 test/dummy/node_modules/array-uniq/index.js
disco_app-0.18.2 test/dummy/node_modules/array-uniq/index.js
disco_app-0.16.1 test/dummy/node_modules/array-uniq/index.js
disco_app-0.15.2 test/dummy/node_modules/array-uniq/index.js
disco_app-0.18.4 test/dummy/node_modules/array-uniq/index.js
disco_app-0.18.1 test/dummy/node_modules/array-uniq/index.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/array-uniq/index.js
disco_app-0.14.0 test/dummy/node_modules/array-uniq/index.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/array-uniq/index.js
tang-0.2.1 spec/tang_app/node_modules/array-uniq/index.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/array-uniq/index.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/array-uniq/index.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/array-uniq/index.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/array-uniq/index.js
tang-0.2.0 spec/tang_app/node_modules/array-uniq/index.js
tang-0.1.0 spec/tang_app/node_modules/array-uniq/index.js
tang-0.0.9 spec/tang_app/node_modules/array-uniq/index.js
enju_library-0.3.8 spec/dummy/node_modules/array-uniq/index.js
ilog-0.4.1 node_modules/array-uniq/index.js