Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert source files to use ES6 classes #59

Merged
merged 2 commits into from
Mar 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions src/array/array.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
'use strict';

var array = {};

/**
* Returns the first value in the given array that isn't undefined.
* @param {!Array} arr
* @return {*}
*/
array.firstDefinedValue = function(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== undefined) {
return arr[i];
class array {
/**
* Returns the first value in the given array that isn't undefined.
* @param {!Array} arr
* @return {*}
*/
static firstDefinedValue(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] !== undefined) {
return arr[i];
}
}
}
};

/**
* Transforms the input nested array to become flat.
* @param {Array.<*|Array.<*>>} arr Nested array to flatten.
* @param {Array.<*>} opt_output Optional output array.
* @return {Array.<*>} Flat array.
*/
array.flatten = function(arr, opt_output) {
var output = opt_output || [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
array.flatten(arr[i], output);
} else {
output.push(arr[i]);
/**
* Transforms the input nested array to become flat.
* @param {Array.<*|Array.<*>>} arr Nested array to flatten.
* @param {Array.<*>} opt_output Optional output array.
* @return {Array.<*>} Flat array.
*/
static flatten(arr, opt_output) {
var output = opt_output || [];
for (var i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
array.flatten(arr[i], output);
} else {
output.push(arr[i]);
}
}
return output;
}
return output;
};

/**
* Removes the first occurrence of a particular value from an array.
* @param {Array.<T>} arr Array from which to remove value.
* @param {T} obj Object to remove.
* @return {boolean} True if an element was removed.
* @template T
*/
array.remove = function(arr, obj) {
var i = arr.indexOf(obj);
var rv;
if ( (rv = i >= 0) ) {
array.removeAt(arr, i);
/**
* Removes the first occurrence of a particular value from an array.
* @param {Array.<T>} arr Array from which to remove value.
* @param {T} obj Object to remove.
* @return {boolean} True if an element was removed.
* @template T
*/
static remove(arr, obj) {
var i = arr.indexOf(obj);
var rv;
if ( (rv = i >= 0) ) {
array.removeAt(arr, i);
}
return rv;
}
return rv;
};

/**
* Removes from an array the element at index i
* @param {Array} arr Array or array like object from which to remove value.
* @param {number} i The index to remove.
* @return {boolean} True if an element was removed.
*/
array.removeAt = function(arr, i) {
return Array.prototype.splice.call(arr, i, 1).length === 1;
};
/**
* Removes from an array the element at index i
* @param {Array} arr Array or array like object from which to remove value.
* @param {number} i The index to remove.
* @return {boolean} True if an element was removed.
*/
static removeAt(arr, i) {
return Array.prototype.splice.call(arr, i, 1).length === 1;
}
}

export default array;
Loading