View filledArray(n, funcForEachIndex).js
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
// REPLACED BY: Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) ) | |
// https://gist.github.com/DarrenSem/bf49d0c87083301a88111b5d444f0a5f |
View Array.build.js
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
// Array.build.js -- sugar for functional equivalent of new Array( length, functionUsingIndex ) aka fillArray(L, F) -- returns Array(length).fill(0).map( (_, i) => callbackFn(i) ) | |
Array.build ||= (length, callbackFn) => ( | |
Array(length|0) | |
.fill(0) | |
.map( (_, i) => | |
// default to just the index [0, 1, 2, ...length-1] | |
( callbackFn || Number )(i) | |
) | |
); |
View negativeZero.js
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
// negativeZero.js - tiny JavaScript function to differentiate -0 from +0 (can process any type other than Symbol or BigInt) | |
// 15 chars: N=a=>1/0==1/-a; | |
var negativeZero = v => Infinity === 1 / -v; // => v === 0 && Number.NEGATIVE_INFINITY === 1 / v; | |
var isWeb = Date.now; // JScript is missing this method | |
var values = [ |
View truncate.js
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
// truncate.js (string, max) => "Ellipses after 1st half of ...then the second half of it." [2000 = default for max if omitted] | |
// 101 chars: | |
// T=(a,b=2e3)=>null==a?"":(a=a+"").length>(b=Math.max(b,5))?a.slice(0,b/2-1)+"..."+a.slice(-b/2+1.5):a; | |
var truncate = (string, max = 2000) => ( | |
string == null ? "" | |
: ( string = String(string) ).length > ( max = Math.max(max, 5) ) | |
? ( | |
string.slice(0, max / 2 - 1) + "..." + string.slice(-max / 2 + 1.5) | |
) |
View JSONstringify.js
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
// JSONstringify.js - from-scratch implementation of JSON.stringify as a coding exercise ( and for JScript WSH using CSCRIPT.EXE /nologo MyJScript.js ) | |
// 525 chars _=function(a){var b,c=null,d=typeof a,e=/^(undefined|function|bigint|symbol)$/i,f=0,g=[],h=function(a,b){return a==b?a+"":{}.toString.call(a).slice(8,-1)};if(!e.test(d)){if("string"===d)return"\""+a+"\"";if(!a||"object"!==d||"Number"===h(a))return("Number"===h(a)?isFinite(a)?+a:c:a)+"";if("Date"===h(a)&&!isNaN(a))return _(a.toISOString());if("Array"===h(a)){for(b=a.length;f<b;f++)g.push(_(e.test(h(a[f]))?c:a[f]));return"["+g+"]"}for(f in a)a.hasOwnProperty(f)&&!e.test(h(a[f]))&&g.push(_(f)+":"+_(a[f]));return"{"+g+"}"}} | |
var JSONstringify = function(obj) { | |
var NULL = null; | |
var type = typeof obj; | |
var skip = /^(undefined|function|bigint|symbol)$/i | |
var L; | |
var key = 0; | |
var elements = []; |
View stringify.js
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
// stringify.js (handles virtually all types unlike the normal JSON.stringify -- intended for inspecting/displaying object values, NOT for serializing/exporting) | |
/* | |
this function's result is far more flexible, human-friendly, and USEFUL than JSON.stringify, because... | |
( Infinity ) => 'Infinity' and ( NaN ) => 'NaN' , instead of 'null' | |
( undefined ) => 'undefined' and ( Date ) => 'function Date() { [native code] }' , instead of undefined | |
( new TypeError("xyz") ) => 'TypeError: xyz' , instead of '{}' | |
( /z/img ) => '/z/gim' , instead of '{}' ( JSCRIPT => '/z/igm' ) | |
( [ , ] ) => '[undefined]' , instead of '[null]' |
View type.js AKA clazz constructor ctor.js
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
// type.js AKA clazz constructor ctor.js (more granular than built-in typeof which returns "object" for most things) | |
// T=a=>({}).toString.call(a).slice(8,-1); // use case: if you have no need for JScript support | |
// T=function(a){return(null==a?a+"":{}.toString.call(a).slice(8,-1))}; // use case: non-JScript returns the same as JScript -- "null"/"undefined" instead of "Null"/"Undefined" | |
// T=function(a){return(/*@cc_on @if(@_jscript) null==a?a+"": @end@*/{}.toString.call(a).slice(8,-1))}; // use case: only JScript returns "null"/"undefined", while non-JScript returns "Null"/"Undefined" | |
var type = function(v) { | |
return ( | |
/*@cc_on @if(@_jscript) v == null ? String(v) : @end@*/ | |
{}.toString.call(v).slice(8,-1) // smaller than Object.prototype.toString.call(v).slice(8,-1) |
View camelToKebab.js
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
// camelToKebab.js (string, toSnakeNotKebab) -- also will recognize automatically when to call REVERSE function | |
// 207 characters var camelToKebab=(a,b,c=/[_-]/.test(a))=>a&&a.link?a.replace(/\s/g,"").replace(/[A-Z_-]/g,c?" ":a=>" "+a).replace(/^\s+|\s+$/g,"").toLowerCase().replace(/ ([a-z]*)/g,c?a=>a.charAt(1).toUpperCase()+a.slice(2):b?"_$1":"-$1"):a | |
// /* // (to run JSCRIPT-compatible version, comment out arrow function) | |
var camelToKebab = (string, toSnakeNotKebab, | |
_reverse = /[_-]/.test(string) | |
) => ( |
View REPL.js
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
// REPL.js AKA eval.js -- REPL.js AKA eval.js -- Bookmarklet to append to current webpage a text input field + global-scope [EVAL] button - ALSO evaluates console.[log|error|warn|info|assert] | |
// javascript:void function(){var a=document,b=(b,c)=>Object.assign(a.createElement(b||"button"),c),c=b("pre"),d=b("div",{contentEditable:!0}),e="innerText",f=b(0,{[e]:"EVAL",onclick:(a,b,c,f=new Date().toLocaleTimeString(),g=location,h,i=/\bconsole\s*\.(log|error|warn|info|assert)\s*\(([^)])/g)=>{try{a=d[e],b=(g._=[],(i.test(a)?"let _=location,_log=(...a)=>{_._.push(a.join(\" \"));"+"\nlet m=(String(a[0]).match(/^<([aefgilnorw_ts]{3,6})>$/)||[])[1]||\"log\";console[m](...(a.slice(m==\"assert\"?1:0)))"+"\n};\n":"")+a).replace(i,`_log('<$1>', $2`),c=(0,eval)(b)+"",g._&&(c=g._.concat([c]).join("\n"))}catch(a){c=a.stack||a.message,c+="\n"+b}d[e]+=/[\n\r]/.test(c)?`\n\n/\* ${f}:\n${c.replace(/\*\//g,"* /")}\n*\/`:`\n// ${f}: ${c}`,d.focus()}});[f,c].forEach(({style:a},b)=>{a.border=b?"solid":"ridge",a.background=b?"#0aa":"# |
View require.js
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
// require.js in just 793 chars -- even works from local CSCRIPT.EXE JScript.js (because not everybody runs Node) -- var exports = require('moduleName') | |
(function(a){var b={},c=[],d=function(a,d){var f={exports:{}},g=a.length?e.resolve(a,c[0]):"",h=e.fetch(g),i=null!=h&&eval("[(function(exports,require,module,__filename,__dirname){"+(h+"})][0]"));if(i){b[d]=f.exports,c.unshift(g);try{i(b[d],e,f,g,e.dir(g))}catch(a){}return c.shift(),f.exports}},e=function(a){var c=(a+"").replace(/\\/g,"/").replace(/\.js$/i,""),e=c.toLowerCase();if(a)return e in b?b[e]:d(c,e)};e.resolve=function(a,b){return(/^\.{1,2}/.test(a)?e.dir(b||location.href.replace(/\\/g,"/"))+"/":"")+a+".js"},e.fetch=function(a){try{var b=a.length&&new XMLHttpRequest,c=b&&(b.send(b.open("GET",a,!1)),b.status),d=b&&b.responseText;if(200===c||d.length&&!c)return d}catch(a){}},e.dir=function(a){var b=a.lastIndexOf("/");return 0>b?"":a.slice(0,b)},a.require=a.require||e})(this); | |
(function(globalContext){ | |
var moduleName = "require"; | |
var cache = {}; |
NewerOlder