gs-json
Version:
gs-JSON is a domain agnostic unifying 3D file format for geometric and semantic modelling (hence the 'gs').
236 lines (221 loc) • 8.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* A set of static methods for working with arrays of simple types.
* The arrays can be nested, but they do not contain any objects.
*/
var Arr = exports.Arr = function () {
function Arr() {
_classCallCheck(this, Arr);
}
_createClass(Arr, null, [{
key: "make",
/**
* Make an array of numbers. All elements in the array will have the same value.
* @param length The length of the new array. If length is 0, then an empty array is returned.
* @param value The values in the array.
* @returns The resulting array.
*/
value: function make(length, value) {
if (length === 0) {
return [];
}
return Array.apply(0, new Array(length)).map(function (v, i) {
return value;
});
}
/**
* Make an array of numbers. All elements in the array will be a numerical sequence, 0, 1, 2, 3....
* @param length The length of the new array. If length is 0, then an empty array is returned.
* @returns The resulting array.
*/
}, {
key: "makeSeq",
value: function makeSeq(length) {
if (length === 0) {
return [];
}
return Array.apply(0, new Array(length)).map(function (v, i) {
return i;
});
}
/**
* Check if two nD arrays are equal (i.e. that all elements in the array are equal, ===.).
* If the arrays are unequal in length, false is returned.
* Elements in the array can have any value.
* @param arr1 The first value.
* @param arr2 The second values.
* @returns True or false.
*/
}, {
key: "equal",
value: function equal(arr1, arr2) {
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
return arr1 === arr2;
}
if (arr1.length !== arr2.length) {
return false;
}
for (var i = 0; i < arr1.length; i++) {
if (!this.equal(arr1[i], arr2[i])) {
return false;
}
}
return true;
}
/**
* Find the position of the first occurrence of a specified value in an array.
* The value can be an array (which is not the case for Array.indexOf()).
* If the value is not found or is undefined, return -1.
* If the array is null or undefined, return -1.
* @param value The value, can be a value or a 1D array of values.
* @returns The index in the array of the first occurance of the value.
*/
}, {
key: "indexOf",
value: function indexOf(arr, value) {
if (!Array.isArray(arr)) {
throw new Error("First argument must be a array.");
}
if (!Array.isArray(value)) {
return arr.indexOf(value);
}
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i]) && this.equal(value, arr[i])) {
return i;
}
}
return -1;
}
/**
* Replace all occurrences of a specified value in an array.
* The input array is changed.
* The value can be an array.
* If the value is not found or is undefined, return -1.
* @param old_value The old value to replace.
* @param new_value The new value.
* @param arr The array.
*/
}, {
key: "replace",
value: function replace(arr, old_value, new_value) {
if (!Array.isArray(arr)) {
throw new Error("First argument must be a array.");
}
for (var i = 0; i < arr.length; i++) {
if (this.equal(arr[i], old_value)) {
arr[i] = new_value;
}
}
}
/**
* Take an nD array and flattens it.
* A new array is returned. The input array remains unchanged.
* For example, [1, 2, [3, 4], [5, 6]] will become [1, 2, 3, 4, 5, 6].
* If the input array is undefined, an empty array is returned.
* @param arr The multidimensional array to flatten.
* @returns A new 1D array.
*/
}, {
key: "flatten",
value: function flatten(arr, depth) {
if (arr === undefined) {
return [];
}
return arr.reduce(function (flat, toFlatten) {
if (depth === undefined) {
return flat.concat(Array.isArray(toFlatten) ? Arr.flatten(toFlatten) : toFlatten);
} else {
return flat.concat(Array.isArray(toFlatten) && depth !== 0 ? Arr.flatten(toFlatten, depth - 1) : toFlatten);
}
}, []);
}
/**
* Make a copy of an nD array.
* If the input is not an array, then just return the same thing.
* A new array is returned. The input array remains unchanged.
* If the input array is undefined, an empty array is returned.
* If the input is s sparse array, then the output will alos be a sparse array.
* @param arr The nD array to copy.
* @returns The new nD array.
*/
}, {
key: "deepCopy",
value: function deepCopy(arr) {
if (arr === undefined) {
return [];
}
if (!Array.isArray(arr)) {
return arr;
}
var arr2 = [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
arr2[i] = Arr.deepCopy(arr[i]);
} else {
if (arr[i] !== undefined) {
arr2[i] = arr[i];
}
}
}
return arr2;
}
/**
* Fills an nD array with new values (all the same value).
* The input array is changed.
* If the input array is undefined, an empty array is returned.
* The input can be a sparse array.
* @param arr The nD array to fill.
* @param value The value to insert into the array.
*/
}, {
key: "deepFill",
value: function deepFill(arr, value) {
if (arr === undefined) {
return;
}
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
Arr.deepFill(arr[i], value);
} else {
if (arr[i] !== undefined) {
arr[i] = value;
}
}
}
}
/**
* Counts the number of values in an nD array .
* The input array remains unchanged.
* If the input array is undefined, 0 is returned.
* The input can be a sparse array. Undefined values are ignored.
* For example, for [1, 2, , , 3], the count will be 3.
* @param arr The nD array to count.
* @return The number of elements in the nD array.
*/
}, {
key: "deepCount",
value: function deepCount(arr) {
if (arr === undefined) {
return 0;
}
var a = 0;
for (var i in arr) {
if (Array.isArray(arr[i])) {
a = a + Arr.deepCount(arr[i]);
} else {
if (arr[i] !== undefined) {
a = a + 1;
}
}
}
return a;
}
}]);
return Arr;
}();
//# sourceMappingURL=arr.js.map