Skip to content

Instantly share code, notes, and snippets.

@jaycliff
Last active August 29, 2015 14:13
Show Gist options
  • Save jaycliff/8c8f3ee8582c2e741bd0 to your computer and use it in GitHub Desktop.
Save jaycliff/8c8f3ee8582c2e741bd0 to your computer and use it in GitHub Desktop.
/*
Copyright 2015 Jaycliff Arcilla
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*jslint bitwise: true */
if (typeof Number.isNaN !== "function") {
Number.isNaN = function (value) {
"use strict";
//return (typeof value === "number" && value !== value);
//return (typeof value === "number" && Number.prototype.toString.call(value) === 'NaN');
return (typeof value === "number" && isNaN(value));
};
}
if (typeof Array.prototype.insertAt !== "function") {
Array.prototype.insertAt = function insertAt(index, item) {
"use strict";
var i;
index = index | 0;
if (index < 0) {
index = 0;
} else if (index > this.length) {
index = this.length;
}
switch (index) {
case this.length:
return this.push(item);
case 0:
return this.unshift(item);
default:
for (i = this.length; i > index; i -= 1) {
this[i] = this[i - 1];
}
this[index] = item;
return this.length;
}
};
}
if (typeof Array.prototype.removeAt !== "function") {
Array.prototype.removeAt = function removeAt(index) {
"use strict";
var item,
i,
length = this.length,
last_index = length - 1,
update = false;
// Always coerce non-number values to the number type
index = index | 0;
if (index >= 0) {
if (index < length) {
item = this[index];
update = true;
}
} else {
index = length + index;
if (index > -1) {
item = this[index];
update = true;
}
}
if (update) {
for (i = index; i < last_index; i += 1) {
this[i] = this[i + 1];
}
this.length = last_index;
}
return item;
};
}
if (typeof Array.prototype.removeItemsByValue !== "function") {
Array.prototype.removeItemsByValue = function (value) {
"use strict";
var i;
if (Number.isNaN(value)) {
i = this.length;
while (i !== 0) {
i -= 1;
if (Number.isNaN(this[i])) {
this.removeAt(i);
}
}
} else {
i = this.indexOf(value);
while (i !== -1) {
this.removeAt(i);
i = this.indexOf(value);
}
}
return this.length;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment