Last active
September 27, 2020 09:17
-
-
Save disco0/f88083b3a112de3e254f004401396de1 to your computer and use it in GitHub Desktop.
Array#join Override
This file contains hidden or 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
//#region Types | |
// Just here to validate that the new signature is identical to the es5 type declaration | |
type Array$join$primordial_Signature = (separator?: string) => string | |
declare interface Array<T> | |
{ | |
separator?: string; | |
join: Array$join$primordial_Signature; | |
join$$primordial: Array$join$primordial_Signature | |
} | |
//#endregion Types | |
//#region Variables | |
// Store original method | |
const Array$join$primordial: Array$join$primordial_Signature = Array.prototype.join; | |
// Absolute fallback for separator (same as original prototype method) | |
let originalSeparator = ','; | |
// New configurable separator parameter default value | |
let defaultSeparator = ' '; | |
//#endregion Variables | |
//#region New `join` | |
function Array$join(this: Array<any>, separator?: string): string | |
{ | |
return Array$join$primordial.bind(this)(separator ?? this.separator ?? originalSeparator); | |
} | |
//#endregion New `join` | |
//#region Binder | |
function bindBuiltinExtension() | |
{ | |
// Array.prototype.separator = defaultSeparator | |
// Array.prototype.join = Array$join; | |
Object.assign(Array.prototype, { | |
separator: defaultSeparator, | |
join: Array$join, | |
join$$primordial: Array$join$primordial | |
}) | |
}; | |
//#endregion Binder |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment