Skip to content

Instantly share code, notes, and snippets.

@danieloneill
Created September 11, 2023 05:57
Show Gist options
  • Save danieloneill/e82445aa864187bdf373badec9c6d87e to your computer and use it in GitHub Desktop.
Save danieloneill/e82445aa864187bdf373badec9c6d87e to your computer and use it in GitHub Desktop.
Qt Quick polyfills for Array.flat, String.endsWith, and String.startsWith
.import 'polyjank.js' as PJ
import 'polyjank.js' as PJ
.pragma library
Array.prototype.flat = function(depthArg=1) {
const getElems = function(arr, elems=[]) {
for( let a of arr )
{
if( typeof a === 'object' )
elems = getElems(a, elems);
else
elems.push(a);
}
return elems;
};
// depthArg is ignored because who even uses it anyway.
return getElems(this);
}
String.prototype.endsWith = function(wut) {
return this.substr( this.length-wut.length ) === wut;
}
String.prototype.startsWith = function(wut) {
return this.substr( 0, wut.length ) === wut;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment