Skip to content

Instantly share code, notes, and snippets.

@beatak
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save beatak/aae9f1f85f2bb16a63dd to your computer and use it in GitHub Desktop.

Select an option

Save beatak/aae9f1f85f2bb16a63dd to your computer and use it in GitHub Desktop.
merge and uniqify_array that I implemented. slow.
diff --git b/lib/utility.js a/lib/utility.js
new file mode 100644
index 0000000..91a03ff
--- /dev/null
+++ a/lib/utility.js
@@ -0,0 +1,72 @@
+/*jslint node: true */
+'use strict';
+
+var uniqify_array = function(arr) {
+ var result, _er;
+ if (!Array.isArray(arr)) {
+ _er = new Error('The given argument is not an array.');
+ _er.name = 'ArgumentError';
+ throw _er;
+ }
+ result = {};
+ arr.forEach(function(key) {
+ result[key] = true;
+ });
+ return Object.keys(result);
+};
+
+/**
+ * one demensional merge, takes any numbers of aruments. right most argument precedes.
+ */
+var merge = function (def) {
+ var result, len, i, deleting_keys, updates, update, _er,
+ keys = [];
+ if ('object' !== typeof def || Array.isArray(def)) {
+ _er = new Error('You need to pass an object, at least for the first argument.');
+ _er.name = 'ArgumentError';
+ throw _er;
+ }
+ if (null === def) {
+ _er = new Error('You need to pass an object, not null.');
+ _er.name = 'ArgumentError';
+ throw _er;
+ }
+
+ result = JSON.parse(JSON.stringify(def));
+ updates = [];
+ Array.prototype.slice.apply(arguments, [1]).forEach(function (elm) {
+ if ('object' === typeof elm) {
+ updates.push(JSON.parse(JSON.stringify(elm)));
+ }
+ });
+ len = updates.length;
+ for (i = 0; i < len; ++i) {
+ keys = keys.concat(Object.keys(updates[i]));
+ }
+ keys = uniqify_array(keys);
+
+ updates.reverse();
+ loop:for (i = 0; i < len; ++i) {
+ deleting_keys = [];
+ update = updates[i];
+ keys.forEach(function (key) {
+ if (undefined !== update[key]) {
+ deleting_keys.push(key);
+ result[key] = update[key];
+ }
+ });
+ deleting_keys.forEach(function (key) {
+ keys.splice(keys.indexOf(key), 1);
+ });
+ if (0 === keys.length) {
+ break loop;
+ }
+ }
+
+ return result;
+};
+
+module.exports = {
+ uniqify_array: uniqify_array,
+ merge: merge
+};
diff --git b/test/utility/utility.js a/test/utility/utility.js
new file mode 100644
index 0000000..b10bc16
--- /dev/null
+++ a/test/utility/utility.js
@@ -0,0 +1,200 @@
+/*jslint node: true */
+'use strict';
+
+var assert = require('assert');
+var fs = require('fs');
+var path = require('path');
+var utility = require(path.join(__dirname, '..', '..', 'lib', 'utility.js'));
+
+var mock = {
+ uniqify_array: {
+ deepEqual: {
+ 'should uniqify one multiple values': [
+ ['a', 'a', 'a'],
+ ['a']
+ ],
+ 'should uniqify multi- multiple values': [
+ ['a', 'a', 'b', 'c', 'c'],
+ ['a', 'b', 'c']
+ ],
+ 'should uniqify the array of numbers into strings...': [
+ [1, 1, 1],
+ ['1']
+ ]
+ },
+ notStrictEqual: {
+ 'should NOT much between arrays of numbers': [
+ [1, 1, 1],
+ JSON.stringify([1])
+ ]
+ },
+ 'throws': {
+ 'should fail when you pass a number': [
+ 1,
+ /ArgumentError/
+ ],
+ 'should fail when you pass a string': [
+ 'hello',
+ /ArgumentError/
+ ],
+ 'should fail when you pass a boolean': [
+ true,
+ /ArgumentError/
+ ],
+ 'should fail when you pass a null': [
+ null,
+ /ArgumentError/
+ ],
+ 'should fail when you pass an object': [
+ {},
+ /ArgumentError/
+ ],
+ 'should fail when you pass an undefined': [
+ undefined,
+ /ArgumentError/
+ ],
+ 'should fail when you pass a function': [
+ function () {return 1;},
+ /ArgumentError/
+ ]
+ }
+ },
+ merge: {
+ deepEqual: {
+ 'should output the same passing one obj': [
+ [{a: 0}],
+ {a: 0}
+ ],
+ 'should merge two objects into one': [
+ [{a: 0}, {b: 1}],
+ {a: 0, b: 1}
+ ],
+ 'should merge three objects into one': [
+ [{a: 0}, {b: 1}, {c: 2}],
+ {a: 0, b: 1, c: 2}
+ ],
+ 'should merge objects with the right-precedent way': [
+ [{a: 0}, {a: 1}, {a: 2}],
+ {a: 2}
+ ],
+ 'should ignore primitive values': [
+ [{a: 0}, 0, 'bar', undefined],
+ {a: 0}
+ ],
+ 'should only merge one-dimensional': [
+ [{a: {aa: 0, ab: 1}}, {a: {ac: 2}}],
+ {a: {ac: 2}}
+ ]
+ },
+ 'throws': {
+ 'should fail when you pass a number as a first arg': [
+ [1],
+ /ArgumentError/
+ ],
+ 'should fail when you pass a string as a first arg': [
+ ['hello'],
+ /ArgumentError/
+ ],
+ 'should fail when you pass a boolean as a first arg': [
+ [true],
+ /ArgumentError/
+ ],
+ 'should fail when you pass a null as a first arg': [
+ [null],
+ /ArgumentError/
+ ],
+ 'should fail when you pass an undefined as a first arg': [
+ [undefined],
+ /ArgumentError/
+ ],
+ 'should fail when you pass a function as a first arg': [
+ [function () {return 1;}],
+ /ArgumentError/
+ ]
+ },
+ strictEqual: {
+ 'should drop non-JSON compliant attributes': [
+ [{a: 0, b: function () {return 1;}}],
+ JSON.stringify({a: 0})
+ ],
+ 'should drop non-JSON compliant attributes even for second arg': [
+ [{a: 0}, {b: function () {return 1;}}],
+ JSON.stringify({a: 0})
+ ]
+ }
+ }
+};
+
+describe('uniqify array', function () {
+ var mock_data = mock.uniqify_array;
+ Object.keys(mock_data).forEach(function (method) {
+ var suite = mock_data[method];
+ Object.keys(suite).forEach(function (should_explain) {
+ var arr = suite[should_explain];
+ switch(method) {
+ case 'throws':
+ it(should_explain, function () {
+ assert[method](
+ function () {
+ utility.uniqify_array(arr[0]);
+ },
+ arr[1]
+ );
+ });
+ break;
+ case 'deepEqual':
+ it(should_explain, function () {
+ assert[method](
+ utility.uniqify_array(arr[0]),
+ arr[1]
+ );
+ });
+ break;
+ case 'notStrictEqual':
+ it(should_explain, function () {
+ assert[method](
+ JSON.stringify(utility.uniqify_array(arr[0])),
+ arr[1]
+ );
+ });
+ break;
+ }
+ });
+ });
+});
+
+describe('merge', function () {
+ var mock_data = mock.merge;
+ Object.keys(mock_data).forEach(function (method) {
+ var suite = mock_data[method];
+ Object.keys(suite).forEach(function (should_explain) {
+ var arr = suite[should_explain];
+ switch(method) {
+ case 'throws':
+ it(should_explain, function () {
+ assert[method](
+ function () {utility.merge.apply(utility, arr[0]);},
+ arr[1]
+ );
+ });
+ break;
+ case 'deepEqual':
+ it(should_explain, function () {
+ assert[method](
+ utility.merge.apply(utility, arr[0]),
+ arr[1]
+ );
+ });
+ break;
+ case 'strictEqual':
+ it(should_explain, function () {
+ assert[method](
+ JSON.stringify(utility.merge.apply(utility, arr[0])),
+ arr[1]
+ );
+ });
+ break;
+ }
+ });
+ });
+});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment