fengari
fengari copied to clipboard
string.format() doesn't work under IE 11
Fengari is said to be "Verified to work in Microsoft IE 11". But unfortunately an error is raised under IE 11:
print(("%02X"):format(0)) TypeError: Object doesn't support property or method 'repeat'

It seems that the sprintf.js which implements string.format is not IE11-compatible.
The problem is in the single line of code:
pad = ph.width ? (pad_length > 0 ? pad_character.repeat(pad_length) : '') : ''
Unfortunately, repeat is not implemented on all IE, including IE11
BTW .repeat occurs also in lstrlib.js.
Support of IE11 actually means support of Windows 7 users. There are a lot of them nowadays.
All what is needed for happiness is to implement String.prototype.repeat manually in Fengari.
Note to self: see core-js implementation https://github.com/zloirock/core-js/blob/master/packages/core-js/internals/string-repeat.js#L12
Filed https://github.com/alexei/sprintf.js/issues/182 Also see #149
I can't modify the global environment
But why? Defining standard well-known (but undefined on IE) function looks very innocent.
I've worked in environments before where the rule is never modify the global JS environment. I want fengari to be allowed in such environments.
Every modification you make is potentially conflicting with some other piece of JS running on a site. The easiest solution is to just opt out.
Probably, this issue should be migrated from fengari to fengari-web, because it affects only fengari running inside browser.
The bug hasn't been solved in three months. Maybe, the documentation should honestly claim that Fengari does require a polyfill for IE11?
I've been hoping that https://github.com/alexei/sprintf.js/pull/183 will get merged.
Otherwise I may just remove that dependency entirely and implement our own sprintf-alike.
I haven't had the time to implement out own sprintf, so I'm marking this issue as "help-wanted". If anyone would like to implement it, please send a PR!
Would it be fine just to fork + vendor sprintf.js with your fix?
Would it be fine just to fork + vendor sprintf.js with your fix?
I guess. But there is plenty of code in there we don't use. And the verification code in lstrlib.js sort of duplicates some of the work anyway. A rewrite with a bit of "inspiration" is probably the best choice.
I can't modify the global environment
But why? Defining standard well-known (but undefined on IE) function looks very innocent.
You pollute users namespace. It's considered a bad practice. It's much easier to point users that it's not working on this and that browser and offer a link to a polyfill - if user wants then can include polyfil from separate repo - but then it's extra request or extra job to bake it into users code.
There is a way to create a copy of String prototype and assign it to new class - OurString and decorate it encapsulated only in module scope. @daurnimator what do you think?
or https://github.com/alexei/sprintf.js/pull/193