Skip to content

Instantly share code, notes, and snippets.

@majuric
Last active November 22, 2015 16:51
Show Gist options
  • Save majuric/8ca0bda0ae30a4df5080 to your computer and use it in GitHub Desktop.
Save majuric/8ca0bda0ae30a4df5080 to your computer and use it in GitHub Desktop.
A short example of the flatten array utility implementation. The code can be extended by adding private or public methods easily. Also the code is compatible with AMD in CommonJS .. tests do not use any unit library, only simple asserts to show the code is testable.
'use strict';
// http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define([], factory);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = factory();
} else {
// Browser globals (root is window)
root.iCom = factory();
}
}(this, function() {
// Add utility methods here that will not be exposed globally
var isArray = function(object) {
return toString.call(object) === '[object Array]';
}
/**
*
* ...
* ...
* Add what ever utility methods necessary easily below
* ...
*/
// Add methods that will be exposed to external use of the library here
return {
flatten: function(array) {
// return if empty
if (!array) return undefined;
// Take only an array into consideration
if (!isArray(array)) return undefined;
var flattened = [];
for (var i = 0; i < array.length; i++) {
if (isArray(array[i])) {
flattened = flattened.concat(this.flatten(array[i]));
} else {
flattened.push(array[i]);
}
}
return flattened;
},
// Utility used for test to compare if the arrays are exact
arraysEqual: function(array1, array2) {
if (array1 === array2) return true;
if (array1 == null || array2 == null) return false;
if (array1.length != array2.length) return false;
for (var i = 0; i < array1.length; ++i) {
if (array1[i] !== array2[i]) return false;
}
return true;
}
/**
* Add what ever method that are required to be externaly exposed
*/
}
}));
// A few basic unit tests but not using a unit test library though one could be easily
// added
var expectations = {
'UNDEFINED': undefined,
'TEST_ARRAY_1': [1, 2, 3, [4, 5, 6, [7, 8, 9], 10, 11, 12]],
'TEST_ARRAY_2': [[[1, [2, 3]]],[[4], 5, 6, [7, 8], 9, 10],[11, 12]],
'EXPECT_ARRAY': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}
// Test the flatten functionality
if (iCom.flatten('a string') === expectations.UNDEFINED) {
console.log('SUCCESS - can\'t use string');
} else {
console.log('FAIL - string ');
}
if (iCom.flatten({}) === expectations.UNDEFINED) {
console.log('SUCCESS - can\'t use object');
} else {
console.log('FAIL - object ');
}
if (iCom.flatten(null) === expectations.UNDEFINED) {
console.log('SUCCESS - can\'t use null');
} else {
console.log('FAIL - null ');
}
if (iCom.flatten([]).length === 0 ) {
console.log('SUCCESS - Returned empty array');
} else {
console.log('FAIL - did not return empty array ');
}
// Check if flatten works with valid array
if (iCom.arraysEqual(iCom.flatten(expectations.TEST_ARRAY_1), expectations.EXPECT_ARRAY)) {
console.log('SUCCESS - array 1');
}
// Check if flatten works with valid array. The members of the array are the same as in
// the above example, but the position and depth of members is not
if (iCom.arraysEqual(iCom.flatten(expectations.TEST_ARRAY_2), expectations.EXPECT_ARRAY)) {
console.log('SUCCESS - array 2');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment