string_prototypes
Version:
A collection of prototype functions for String object, some of them already adopted by new standards as ES6
64 lines (55 loc) • 2.13 kB
HTML
<HTML>
<BODY>
<DIV id=oDiv style="width: 100%; height: 640px; position: relative; display: block;">
</DIV>
<SCRIPT src=index.js></SCRIPT>
<SCRIPT>
Object.prototype.toString = function () {
var res="{ ";
for (var l in this){
if (! ((this[l] instanceof Function)
|| (this[l] instanceof Array)) ) res +='<br>'+ l+" : "+this[l];
}
return res+" } ";
}
var module = _String_Prototypes.apply(window)
, _out = document.getElementById('oDiv')
, log = function () {
var a=arguments;
for (var l in arguments){
_out.innerHTML+='<br>'+a[l];
}
}
, out = function (s) { _out.innerHTML+= s; }
, logNl = function () { log('<br>'); }
, test = function (tst,res) {
var evl=eval(tst);
var fmt='<tr><td>'+tst+'</td><td>['+res+']</td><td>['+evl+']</td><td>'+(evl==res?'pass':'FAIL')+'</td></tr>';
return fmt;
}
;
log (String.prototype.trim
,String.prototype.repeat
,String.prototype.startsWith
,String.prototype.endsWith
,String.prototype.includes
,'<hr>'
);
var assert = {}
assert.strictEquals = function () { tabl+=Function.apply.call(test,window,arguments); }
log (module);
logNl ();
var tabl='<pre><table>';
tabl+='<tr><th width=200>test</th><th width=200>expected</th><th width=200>result</th><th>check</th></tr>';
assert.strictEquals("'foo'.repeat(3)" , 'foofoofoo' );
assert.strictEquals("'\t\xA0\uFEFF 123 \t'.trim()" , '123' );
var s='from ->->> to';
assert.strictEquals("s.startsWith ('from')" , true);
assert.strictEquals("s.endsWith ('to')" , true);
assert.strictEquals("s.includes ('->->>')" , true);
tabl+='</table></pre>';
log(tabl);
</SCRIPT>
</BODY>
</HTML>