Last active
September 21, 2023 14:04
-
-
Save Araxeus/4e752d4273e6d50c8977011c31488c34 to your computer and use it in GitHub Desktop.
Array[`replace`/`replaceAll`/`groupBy`]
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
const defineArr = (prop, value) => { | |
if (!Array.prototype.hasOwnProperty(prop)) { | |
Object.defineProperty(Array.prototype, prop, { | |
value | |
}); | |
} | |
}; | |
defineArr('groupBy', function value(fn) { return this.reduce((p, c, i) => { | |
const k = fn(c, i, p); | |
return { ...p, [k]: [...(p[k] || []), c] }; | |
}, {}) }); | |
defineArr('replace', function value(filter, value) { | |
const cpy = [...this]; | |
cpy[cpy.findIndex(filter)] = value; | |
return cpy; | |
}); | |
defineArr('replaceAll', function value(filter, value) { | |
return this.map((val, indx, arr) => (filter(val, indx, arr) ? value : val)); | |
}); | |
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
/* | |
https://www.typescriptlang.org/play?target=6#code/C4TwDgpgBAShYBsCGBjCAnAYgVwHYuAEsB7XAHgBUA+KAXigApgALQgZwC4oKBtAXQA0UAGaEEwDFwYA3JAmwQuFIYVwATCAA8uubAFsARhiFJ06JCCX8AlHRp4A1rmIB3XENnzF3W7Rq8+AChAjRRkdGhVCXRhVGgAQTMLShoAb0CoTKgIxDiuOFy0LDwCEnJqAG4MrJzkNHiEBHz4OowcfCJSFKqAX2DE8xAAOjB0YmBx8Agh2rioAH55+mESztxGFnYhUXFjKE8FW3SsqBRSNmBTsBA6KB4hh822PiqTlGued+HRdQBJdS0DB20WsfFuBwgrxqEGA2HQ6y+vSqgQGFhGYwmoEgMxacQaCAWSxEqzKG1YbG2YmiHjkhygx2hsPhUCeQz0SDADBkchU6k0JjMvhowIw3IQvLU-KgpnQtnm+1p0C4nmsgR6yIA9AAqFFJYajcaTbGzeqNW4rDqklIMaqZJ5WQS2kRUySMCFKCVaHT6IzoAWDB1CqCOZxuARO93cQJHJ0RJnrVnszliiVSmVBkXoFNQVSS-1yhVeKDKuSq9WBLUa4KBM64C5QL1QVEgMhIXAgGj0HgARiEAGYhAA2IQAFhewVr9YQxAA5rcGA8hqYZ5xpe2g5PiAhptOZwxl2w2RyuUguG2QEGAOQAHVwl6gAGooAApADKAHkAHJDC7oVQzwhhBAfdrGsIYJlfYA-1wPdQOCXcGC0IRL2gGgAFY7EvIQtBxQoIEQuwGygdDkJNCA1EvOCEKQqAUKgABSKAACYoAAQnoAAGTDsM0XDWnxAi-CIximNI3E0AouDgg1DVuGIKBt1MdY9GICJpQMYhsEuFhoGQGDsCQGcICEMJCBQBx1OIaRInWAAiABRTQkD0RAIDYWyoFUqBbIAdWYJBgEvNgoE-CAXFsoZAhkqB3x09AXHYYyoCMy4LlMCQ1CgAwbgiFTpH-FlmDc6Azj0PQIFwYBgrbTKdKgFxVIQTL2CgEBNPQKBchAGcxjwNRIqAA | |
*/ | |
// TypeScript part | |
type ReplacerFunction<T> = ( | |
this: T[], | |
filter: (value: T, index: number, array: T[]) => unknown, | |
value: T | |
) => T[]; | |
declare interface Array<T> { | |
replace: ReplacerFunction<T>; | |
replaceAll: ReplacerFunction<T>; | |
} | |
// JavaScript part | |
Array.prototype.replace ??= function (this, filter, value) { | |
const cpy = [...this]; | |
cpy[cpy.findIndex(filter)] = value; | |
return cpy; | |
}; | |
Array.prototype.replaceAll ??= function (this, filter, value) { | |
return this.map((val, indx, arr) => (filter(val, indx, arr) ? value : val)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment