Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active July 7, 2023 02:46
Show Gist options
  • Save kuanyui/9c2c5cc664c442576a6eae6f8f3f7136 to your computer and use it in GitHub Desktop.
Save kuanyui/9c2c5cc664c442576a6eae6f8f3f7136 to your computer and use it in GitHub Desktop.
Use via `import './enhanced_array'`
export { }
declare global {
interface Array<T> {
last (): T | undefined
delete (elem: T): boolean
pushIfNotExist (elem: T): boolean
}
}
Array.prototype.last = function () {
return this[this.length - 1]
}
Array.prototype.delete = function <T> (elem: T): boolean {
const index = this.indexOf(elem)
if (index === -1) { return false }
this.splice(index, 1)
return true
}
Array.prototype.pushIfNotExist = function <T> (elem: T): boolean {
if (this.includes(elem)) {
return false
} else {
this.push(elem)
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment