Last active
July 7, 2023 02:46
-
-
Save kuanyui/9c2c5cc664c442576a6eae6f8f3f7136 to your computer and use it in GitHub Desktop.
Use via `import './enhanced_array'`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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