From d031be3ea8be7e0ced465ba530aa8f205b7bd055 Mon Sep 17 00:00:00 2001 From: fossabot Date: Thu, 27 Jul 2017 20:42:22 -0700 Subject: [PATCH 01/15] Add license scan report and status --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6a6fbe4..d28af52 100644 --- a/README.md +++ b/README.md @@ -91,9 +91,13 @@ requires access to a C compiler. Please consult the attached LICENSE file for details. All rights not explicitly granted by the Apache 2.0 license are reserved by the Original Author. + +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FSheetJS%2Fprintj.svg?type=large)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FSheetJS%2Fprintj?ref=badge_large) + ## Badges [![Build Status](https://saucelabs.com/browser-matrix/printj.svg)](https://saucelabs.com/u/printj) +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FSheetJS%2Fprintj.svg?type=shield)](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FSheetJS%2Fprintj?ref=badge_shield) [![Build Status](https://travis-ci.org/SheetJS/printj.svg?branch=master)](https://travis-ci.org/SheetJS/printj) @@ -999,5 +1003,4 @@ no attempt is made to correct for them. The test suite ignores those cases: - Parser accepts but does not emulate CRT wide and unicode character conversions - glibc `Z` length conversion and extended `m` error support - Parser fails on CRT `I32`/`I64` fixed lengths -- Default `LP64` data model but can be configured to support `ILP32` or `LLP64` - +- Default `LP64` data model but can be configured to support `ILP32` or `LLP64` \ No newline at end of file -- 2.34.1 From 350978bd98fcefd6f3aaecb719510a08e29d764f Mon Sep 17 00:00:00 2001 From: SheetJS Date: Tue, 16 Jan 2018 19:55:27 -0500 Subject: [PATCH 02/15] version bump 1.1.1: lint --- .eslintrc | 12 ++++++------ Makefile | 9 +++++++++ bin/printj.njs | 2 ++ bits/00_header.js | 2 ++ bits/01_version.js | 2 +- bits/52_convint.js | 2 +- ctest/printj.js | 6 ++++-- dist/printj.js | 8 ++++++-- dist/printj.min.js | 2 +- dist/printj.min.map | 2 +- index.html | 3 +-- package.json | 39 +++++++++++++++++++++++++++++++-------- printj.flow.js | 6 ++++-- printj.js | 6 ++++-- 14 files changed, 73 insertions(+), 28 deletions(-) diff --git a/.eslintrc b/.eslintrc index 3ae0c77..8dc3744 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,14 +5,14 @@ "ecmaVersion": 3, }, "plugins": [ "html", "json" ], - "!extends": "eslint:recommended", + "extends": "eslint:recommended", "rules": { - "no-console": 0, - "no-bitwise": 0, - "curly": 0, "comma-style": [ 2, "last" ], + "comma-dangle": [ 2, "never" ], + "curly": 0, + "no-bitwise": 0, + "no-console": 0, "no-trailing-spaces": 2, - "semi": [ 2, "always" ], - "comma-dangle": [ 2, "never" ] + "semi": [ 2, "always" ] } } diff --git a/Makefile b/Makefile index eef66b3..f76305b 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,9 @@ clean-stress: ## Remove stress tests ## Code Checking +.PHONY: fullint +fullint: lint old-lint tslint flow mdlint ## Run all checks + .PHONY: lint lint: $(TARGET) ## Run eslint checks @eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json bower.json @@ -103,6 +106,12 @@ misc/coverage.html: $(TARGET) test.js coveralls: ## Coverage Test + Send to coveralls.io mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js +MDLINT=README.md +.PHONY: mdlint +mdlint: $(MDLINT) ## Check markdown documents + alex $^ + mdspell -a -n -x -r --en-us $^ + .PHONY: help help: @grep -hE '(^[a-zA-Z_-][ a-zA-Z_-]*:.*?|^#[#*])' $(MAKEFILE_LIST) | bash misc/help.sh diff --git a/bin/printj.njs b/bin/printj.njs index 74920f6..53fd52f 100755 --- a/bin/printj.njs +++ b/bin/printj.njs @@ -1,5 +1,6 @@ #!/usr/bin/env node /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ +/* eslint-env node */ /* vim: set ts=2 ft=javascript: */ /*jshint node:true, evil:true */ var X = require("../"), argv = process.argv; @@ -42,6 +43,7 @@ function parse_arg(arg/*:string*/)/*:any*/ { case /*e*/ 101: return eval(m); case /*s*/ 115: return m; } + void p; return arg; } diff --git a/bits/00_header.js b/bits/00_header.js index 32a09a3..a609499 100644 --- a/bits/00_header.js +++ b/bits/00_header.js @@ -7,6 +7,7 @@ var PRINTJ/*:PRINTJModule*/; (function (factory/*:(a:any)=>void*/)/*:void*/ { /*jshint ignore:start */ + /*eslint-disable */ if(typeof DO_NOT_EXPORT_PRINTJ === 'undefined') { if('object' === typeof exports) { factory(exports); @@ -22,6 +23,7 @@ var PRINTJ/*:PRINTJModule*/; } else { factory(PRINTJ = /*::(*/{}/*:: :any)*/); } + /*eslint-enable */ /*jshint ignore:end */ }(function(PRINTJ/*:PRINTJModule*/) { #include "01_version.js" diff --git a/bits/01_version.js b/bits/01_version.js index 3633660..f9bcb7a 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.1.0'; +PRINTJ.version = '1.1.1'; diff --git a/bits/52_convint.js b/bits/52_convint.js index 328a8ba..76797c8 100644 --- a/bits/52_convint.js +++ b/bits/52_convint.js @@ -7,7 +7,7 @@ /* unsigned integer */ case /*U*/ 85: LONG; /* falls through */ - case /*u*/ 117: INT; break; + case /*u*/ 117: INT; break; /* unsigned octal */ case /*O*/ 79: LONG; diff --git a/ctest/printj.js b/ctest/printj.js index 626c1c7..186254f 100644 --- a/ctest/printj.js +++ b/ctest/printj.js @@ -5,6 +5,7 @@ var PRINTJ; (function (factory) { /*jshint ignore:start */ + /*eslint-disable */ if(typeof DO_NOT_EXPORT_PRINTJ === 'undefined') { if('object' === typeof exports) { factory(exports); @@ -20,10 +21,11 @@ var PRINTJ; } else { factory(PRINTJ = {}); } + /*eslint-enable */ /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.1.0'; +PRINTJ.version = '1.1.1'; function tokenize(fmt) { var out = []; @@ -235,7 +237,7 @@ function doit(t, args) { /* unsigned integer */ case /*U*/ 85: bytes = 8; /* falls through */ - case /*u*/ 117: isnum = -1; break; + case /*u*/ 117: isnum = -1; break; /* unsigned octal */ case /*O*/ 79: bytes = 8; diff --git a/dist/printj.js b/dist/printj.js index 7a65d48..186254f 100644 --- a/dist/printj.js +++ b/dist/printj.js @@ -5,6 +5,7 @@ var PRINTJ; (function (factory) { /*jshint ignore:start */ + /*eslint-disable */ if(typeof DO_NOT_EXPORT_PRINTJ === 'undefined') { if('object' === typeof exports) { factory(exports); @@ -20,10 +21,11 @@ var PRINTJ; } else { factory(PRINTJ = {}); } + /*eslint-enable */ /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.0.1'; +PRINTJ.version = '1.1.1'; function tokenize(fmt) { var out = []; @@ -235,7 +237,7 @@ function doit(t, args) { /* unsigned integer */ case /*U*/ 85: bytes = 8; /* falls through */ - case /*u*/ 117: isnum = -1; break; + case /*u*/ 117: isnum = -1; break; /* unsigned octal */ case /*O*/ 79: bytes = 8; @@ -314,6 +316,8 @@ function doit(t, args) { } + if(width < 0) { width = -width; flags += "-"; } + if(isnum == -1) { Vnum = Number(arg); diff --git a/dist/printj.min.js b/dist/printj.min.js index a24da26..bfa038b 100644 --- a/dist/printj.min.js +++ b/dist/printj.min.js @@ -1,2 +1,2 @@ /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ -var PRINTJ;(function(factory){if(typeof DO_NOT_EXPORT_PRINTJ==="undefined"){if("object"===typeof exports){factory(exports)}else if("function"===typeof define&&define.amd){define(function(){var module={};factory(module);return module})}else{factory(PRINTJ={})}}else{factory(PRINTJ={})}})(function(PRINTJ){PRINTJ.version="1.0.1";function tokenize(fmt){var out=[];var start=0;var i=0;var infmt=false;var fmtparam="",fmtflags="",fmtwidth="",fmtprec="",fmtlen="";var c=0;var L=fmt.length;for(;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?padstr["0"].substr(0,8-O.length):"")+O;O=(16-O.length>=0?padstr["f"].substr(0,16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?padstr["0"].substr(0,10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?padstr["7"].substr(0,21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?padstr["0"].substr(0,prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?padstr["0"].substr(0,prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?padstr["0"].substr(0,prec-O.length):"")+O;pad=width-O.length>=0?padstr[prec>0?" ":"0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?padstr["0"].substr(0,E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?padstr["0"].substr(0,prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?padstr["0"].substr(0,prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?padstr["0"].substr(0,prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?padstr["0"].substr(0,8-O.length):"")+O;O=(16-O.length>=0?padstr["f"].substr(0,16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?padstr["0"].substr(0,10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?padstr["7"].substr(0,21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?padstr["0"].substr(0,prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?padstr["0"].substr(0,prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?padstr["0"].substr(0,prec-O.length):"")+O;pad=width-O.length>=0?padstr[prec>0?" ":"0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?padstr["0"].substr(0,E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?padstr["0"].substr(0,prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?padstr["0"].substr(0,prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?padstr["0"].substr(0,prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i - + + + diff --git a/tests/node.mjs b/tests/node.mjs new file mode 100755 index 0000000..8f0b475 --- /dev/null +++ b/tests/node.mjs @@ -0,0 +1,5 @@ +#!/usr/bin/env node --experimental-modules + +import { version, sprintf } from '../printj.mjs' + +console.log(sprintf("PRINTJ version %s, 123 = 0x%02hhx", version, 123)); diff --git a/tests/printj.mjs b/tests/printj.mjs new file mode 120000 index 0000000..6a4bf14 --- /dev/null +++ b/tests/printj.mjs @@ -0,0 +1 @@ +../printj.mjs \ No newline at end of file -- 2.34.1 From 860f1ed9e64ad826a3e364e68001e6e6c831a3c8 Mon Sep 17 00:00:00 2001 From: pimlie Date: Fri, 19 Oct 2018 10:03:20 +0200 Subject: [PATCH 06/15] feat: use str.repeat for padding (fixes #4) feat: add str.repeat polyfill for older browsers feat: add module field to package.json --- bits/00_header.js | 1 + bits/05_polyfill.js | 38 ++++++++++++++++++++++++++++++++++++++ bits/40_macros.js | 9 +-------- package.json | 1 + 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 bits/05_polyfill.js diff --git a/bits/00_header.js b/bits/00_header.js index 2cd78d2..2829d16 100644 --- a/bits/00_header.js +++ b/bits/00_header.js @@ -10,6 +10,7 @@ var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/; export const version = PRINTJ.version; #else var PRINTJ/*:PRINTJModule*/; +#include "05_polyfill.js" (function (factory/*:(a:any)=>void*/)/*:void*/ { /*jshint ignore:start */ /*eslint-disable */ diff --git a/bits/05_polyfill.js b/bits/05_polyfill.js new file mode 100644 index 0000000..6275ed3 --- /dev/null +++ b/bits/05_polyfill.js @@ -0,0 +1,38 @@ +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat +if (!String.prototype.repeat) { + String.prototype.repeat = function(count) { + 'use strict'; + if (this == null) { + throw new TypeError('can\'t convert ' + this + ' to object'); + } + var str = '' + this; + count = +count; + if (count != count) { + count = 0; + } + if (count < 0) { + throw new RangeError('repeat count must be non-negative'); + } + if (count == Infinity) { + throw new RangeError('repeat count must be less than infinity'); + } + count = Math.floor(count); + if (str.length == 0 || count == 0) { + return ''; + } + // Ensuring count is a 31-bit integer allows us to heavily optimize the + // main part. But anyway, most current (August 2014) browsers can't handle + // strings 1 << 28 chars or longer, so: + if (str.length * count >= 1 << 28) { + throw new RangeError('repeat count must not overflow maximum string size'); + } + var maxCount = str.length * count; + count = Math.floor(Math.log(count) / Math.log(2)); + while (count) { + str += str; + count--; + } + str += str.substring(0, maxCount - str.length); + return str; + } +} diff --git a/bits/40_macros.js b/bits/40_macros.js index 47b4e92..46811bd 100644 --- a/bits/40_macros.js +++ b/bits/40_macros.js @@ -1,12 +1,5 @@ #define isnan isNaN -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr/*:{[s:string]:string}*/ = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; -#define PAD_(x,c) (x >= 0 ? padstr[c].substr(0,x) : "") +#define PAD_(x,c) (x >= 0 ? c.repeat(x) : "") #ifdef DO_NOT_INLINE function pads(x/*:number*/, c/*:string*/)/*:string*/ { return PAD_(x,c); } #define PADS(x,c) pads(x,c) diff --git a/package.json b/package.json index 381c90d..5bfc17b 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "printj": "./bin/printj.njs" }, "main": "./printj", + "module": "./print.mjs", "types": "types", "browser": { "process": false, -- 2.34.1 From f04791d1e754c0af81a31827b432a09aa8a41619 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Fri, 19 Oct 2018 13:13:54 -0400 Subject: [PATCH 07/15] version bump 1.2.1: cleanup --- .gitignore | 1 + bits/00_header.js | 1 - bits/01_version.js | 2 +- bits/05_polyfill.js | 38 ------------------------------ ctest/printj.js | 56 +++++++++++++++++++-------------------------- ctest/shim.js | 39 +++++++++++++++++++++++++++++++ ctest/test.js | 3 +++ lib/loop_code.mjs | 54 +++++++++++++++++++------------------------ package.json | 2 +- printj.flow.js | 54 +++++++++++++++++++------------------------ printj.js | 54 +++++++++++++++++++------------------------ printj.mjs | 54 +++++++++++++++++++------------------------ shim.js | 39 +++++++++++++++++++++++++++++++ test.js | 3 +++ types/bin_printj.ts | 21 +++++++++++++---- types/tsconfig.json | 2 +- types/tslint.json | 2 ++ 17 files changed, 222 insertions(+), 203 deletions(-) delete mode 100644 bits/05_polyfill.js diff --git a/.gitignore b/.gitignore index bc55255..159e4fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules misc/coverage.html ctest/sauce* *.sheetjs +*.tgz diff --git a/bits/00_header.js b/bits/00_header.js index 2829d16..2cd78d2 100644 --- a/bits/00_header.js +++ b/bits/00_header.js @@ -10,7 +10,6 @@ var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/; export const version = PRINTJ.version; #else var PRINTJ/*:PRINTJModule*/; -#include "05_polyfill.js" (function (factory/*:(a:any)=>void*/)/*:void*/ { /*jshint ignore:start */ /*eslint-disable */ diff --git a/bits/01_version.js b/bits/01_version.js index eafee04..aafa57c 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.2.0'; +PRINTJ.version = '1.2.1'; diff --git a/bits/05_polyfill.js b/bits/05_polyfill.js deleted file mode 100644 index 6275ed3..0000000 --- a/bits/05_polyfill.js +++ /dev/null @@ -1,38 +0,0 @@ -// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat -if (!String.prototype.repeat) { - String.prototype.repeat = function(count) { - 'use strict'; - if (this == null) { - throw new TypeError('can\'t convert ' + this + ' to object'); - } - var str = '' + this; - count = +count; - if (count != count) { - count = 0; - } - if (count < 0) { - throw new RangeError('repeat count must be non-negative'); - } - if (count == Infinity) { - throw new RangeError('repeat count must be less than infinity'); - } - count = Math.floor(count); - if (str.length == 0 || count == 0) { - return ''; - } - // Ensuring count is a 31-bit integer allows us to heavily optimize the - // main part. But anyway, most current (August 2014) browsers can't handle - // strings 1 << 28 chars or longer, so: - if (str.length * count >= 1 << 28) { - throw new RangeError('repeat count must not overflow maximum string size'); - } - var maxCount = str.length * count; - count = Math.floor(Math.log(count) / Math.log(2)); - while (count) { - str += str; - count--; - } - str += str.substring(0, maxCount - str.length); - return str; - } -} diff --git a/ctest/printj.js b/ctest/printj.js index a2a38d6..ff000bb 100644 --- a/ctest/printj.js +++ b/ctest/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.1.2'; +PRINTJ.version = '1.2.1'; function tokenize(fmt) { var out = []; @@ -149,14 +149,6 @@ function tokenize(fmt) { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - /*global process:true, util:true, require:true */ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util"); var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; @@ -208,7 +200,7 @@ function doit(t, args) { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -223,7 +215,7 @@ function doit(t, args) { case "string": O = arg.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -308,10 +300,10 @@ function doit(t, args) { /* boolean (extension) */ case /*Y*/ 89: case /*y*/ 121: - O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); + O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -373,16 +365,16 @@ function doit(t, args) { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -403,8 +395,8 @@ function doit(t, args) { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -425,10 +417,10 @@ function doit(t, args) { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -436,7 +428,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -483,8 +475,8 @@ function doit(t, args) { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -498,7 +490,7 @@ function doit(t, args) { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac = O.charCodeAt(0); @@ -545,11 +537,11 @@ function doit(t, args) { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -566,9 +558,9 @@ function doit(t, args) { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -576,7 +568,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/ctest/shim.js b/ctest/shim.js index e3d7840..4493b85 100644 --- a/ctest/shim.js +++ b/ctest/shim.js @@ -235,3 +235,42 @@ if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) { return target; }; } + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat +if (!String.prototype.repeat) { + String.prototype.repeat = function(count) { + 'use strict'; + if (this == null) { + throw new TypeError('can\'t convert ' + this + ' to object'); + } + var str = '' + this; + count = +count; + if (count != count) { + count = 0; + } + if (count < 0) { + throw new RangeError('repeat count must be non-negative'); + } + if (count == Infinity) { + throw new RangeError('repeat count must be less than infinity'); + } + count = Math.floor(count); + if (str.length == 0 || count == 0) { + return ''; + } + // Ensuring count is a 31-bit integer allows us to heavily optimize the + // main part. But anyway, most current (August 2014) browsers can't handle + // strings 1 << 28 chars or longer, so: + if (str.length * count >= 1 << 28) { + throw new RangeError('repeat count must not overflow maximum string size'); + } + var maxCount = str.length * count; + count = Math.floor(Math.log(count) / Math.log(2)); + while (count) { + str += str; + count--; + } + str += str.substring(0, maxCount - str.length); + return str; + }; +} diff --git a/ctest/test.js b/ctest/test.js index b073a51..ef30c84 100644 --- a/ctest/test.js +++ b/ctest/test.js @@ -190,4 +190,7 @@ describe('special cases', function() { assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |"); assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|"); }); + it('handles long strings', function() { + for(var i = 10; i <= 1000; i+= 10) assert.equal(sprintf("%" + i + "s", "abc").length, i); + }); }); diff --git a/lib/loop_code.mjs b/lib/loop_code.mjs index decfc8c..19bd6fb 100644 --- a/lib/loop_code.mjs +++ b/lib/loop_code.mjs @@ -7,7 +7,7 @@ var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/; -PRINTJ.version = '1.2.0'; +PRINTJ.version = '1.2.1'; export const version = PRINTJ.version; @@ -133,14 +133,6 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr/*:{[s:string]:string}*/ = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - var u_inspect/*:(o:any)=>string*/ = JSON.stringify; function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { @@ -190,7 +182,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -205,7 +197,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -293,7 +285,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -355,16 +347,16 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1/*:Array*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -385,8 +377,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -407,10 +399,10 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -418,7 +410,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -465,8 +457,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -480,7 +472,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac/*:number*/ = O.charCodeAt(0); @@ -527,11 +519,11 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -548,9 +540,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -558,7 +550,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/package.json b/package.json index 5bfc17b..50d9ddf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "printj", - "version": "1.2.0", + "version": "1.2.1", "author": "sheetjs", "description": "Pure-JS printf", "keywords": [ diff --git a/printj.flow.js b/printj.flow.js index f912739..ecf88bf 100644 --- a/printj.flow.js +++ b/printj.flow.js @@ -28,7 +28,7 @@ var PRINTJ/*:PRINTJModule*/; /*jshint ignore:end */ }(function(PRINTJ/*:PRINTJModule*/) { -PRINTJ.version = '1.2.0'; +PRINTJ.version = '1.2.1'; function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { var out/*:ParsedFmt*/ = []; @@ -152,14 +152,6 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr/*:{[s:string]:string}*/ = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - /*:: var util = require('util'); */ /*global process:true, util:true, require:true */ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util"); @@ -212,7 +204,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -227,7 +219,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -315,7 +307,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -377,16 +369,16 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1/*:Array*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -407,8 +399,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -429,10 +421,10 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -440,7 +432,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -487,8 +479,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -502,7 +494,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac/*:number*/ = O.charCodeAt(0); @@ -549,11 +541,11 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -570,9 +562,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -580,7 +572,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/printj.js b/printj.js index f01f0bf..ff000bb 100644 --- a/printj.js +++ b/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.0'; +PRINTJ.version = '1.2.1'; function tokenize(fmt) { var out = []; @@ -149,14 +149,6 @@ function tokenize(fmt) { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - /*global process:true, util:true, require:true */ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util"); var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; @@ -208,7 +200,7 @@ function doit(t, args) { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -223,7 +215,7 @@ function doit(t, args) { case "string": O = arg.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -311,7 +303,7 @@ function doit(t, args) { O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -373,16 +365,16 @@ function doit(t, args) { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -403,8 +395,8 @@ function doit(t, args) { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -425,10 +417,10 @@ function doit(t, args) { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -436,7 +428,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -483,8 +475,8 @@ function doit(t, args) { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -498,7 +490,7 @@ function doit(t, args) { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac = O.charCodeAt(0); @@ -545,11 +537,11 @@ function doit(t, args) { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -566,9 +558,9 @@ function doit(t, args) { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -576,7 +568,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/printj.mjs b/printj.mjs index decfc8c..19bd6fb 100644 --- a/printj.mjs +++ b/printj.mjs @@ -7,7 +7,7 @@ var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/; -PRINTJ.version = '1.2.0'; +PRINTJ.version = '1.2.1'; export const version = PRINTJ.version; @@ -133,14 +133,6 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr/*:{[s:string]:string}*/ = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - var u_inspect/*:(o:any)=>string*/ = JSON.stringify; function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { @@ -190,7 +182,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -205,7 +197,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -293,7 +285,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -355,16 +347,16 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1/*:Array*/ = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -385,8 +377,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -407,10 +399,10 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -418,7 +410,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -465,8 +457,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -480,7 +472,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac/*:number*/ = O.charCodeAt(0); @@ -527,11 +519,11 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -548,9 +540,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -558,7 +550,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/shim.js b/shim.js index e3d7840..4493b85 100644 --- a/shim.js +++ b/shim.js @@ -235,3 +235,42 @@ if (typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) { return target; }; } + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat +if (!String.prototype.repeat) { + String.prototype.repeat = function(count) { + 'use strict'; + if (this == null) { + throw new TypeError('can\'t convert ' + this + ' to object'); + } + var str = '' + this; + count = +count; + if (count != count) { + count = 0; + } + if (count < 0) { + throw new RangeError('repeat count must be non-negative'); + } + if (count == Infinity) { + throw new RangeError('repeat count must be less than infinity'); + } + count = Math.floor(count); + if (str.length == 0 || count == 0) { + return ''; + } + // Ensuring count is a 31-bit integer allows us to heavily optimize the + // main part. But anyway, most current (August 2014) browsers can't handle + // strings 1 << 28 chars or longer, so: + if (str.length * count >= 1 << 28) { + throw new RangeError('repeat count must not overflow maximum string size'); + } + var maxCount = str.length * count; + count = Math.floor(Math.log(count) / Math.log(2)); + while (count) { + str += str; + count--; + } + str += str.substring(0, maxCount - str.length); + return str; + }; +} diff --git a/test.js b/test.js index b073a51..ef30c84 100644 --- a/test.js +++ b/test.js @@ -190,4 +190,7 @@ describe('special cases', function() { assert.equal(sprintf("|%*.*s|", 4, 0, "sheetjs"), "| |"); assert.equal(sprintf("|%*.*s|", 4, -1, "sheetjs"), "|sheetjs|"); }); + it('handles long strings', function() { + for(var i = 10; i <= 1000; i+= 10) assert.equal(sprintf("%" + i + "s", "abc").length, i); + }); }); diff --git a/types/bin_printj.ts b/types/bin_printj.ts index f5de475..c1c5d67 100644 --- a/types/bin_printj.ts +++ b/types/bin_printj.ts @@ -1,8 +1,8 @@ /* printj.ts (C) 2016-present SheetJS -- http://sheetjs.com */ /* vim: set ts=2 ft=typescript: */ -/*jshint node:true, evil:true */ +/// import X = require("printj"); -let argv = ["n:1","a","e:null","f:3.4", "b:true", "e:1+1"]; +const argv = ["n:1","a","e:null","f:3.4", "b:true", "e:1+1"]; function help() { [ @@ -10,6 +10,7 @@ function help() { "", "Options:", " -h, --help output usage information", +" -d, --dump print debug information about format string", "", "Arguments are treated as strings unless prefaced by a type indicator:", " n: call parseInt (ex. n:3 -> 3)", @@ -32,22 +33,32 @@ return 0; } function parse_arg(arg: string): any { - let m: string = arg.substr(2), p: number = 0; + const m: string = arg.substr(2); + let p: number = 0; if(arg.charCodeAt(1) === 58) switch((p = arg.charCodeAt(0))) { case /*n*/ 110: return parseInt(m, 10); case /*f*/ 102: return parseFloat(m); case /*b*/ 98: return !(m.toUpperCase() === "FALSE" || m === "0"); case /*j*/ 106: return JSON.parse(m); + case /*e*/ 101: return /*eval*/(m); case /*s*/ 115: return m; } return arg; } -let args: any[] = []; +const args: any[] = []; let fmt = "", n = 0; for(let i = 2; i < argv.length; ++i) switch(argv[i]) { - case "--help": case "-h": break; + case "--help": case "-h": process.exit(help()); break; + case "--dump": case "-d": if(fmt.length===0) fmt = argv[++i]; process.exit(dump(fmt)); break; default: if(n++ === 0) fmt = argv[i]; else args.push(parse_arg(argv[i])); } console.log(X.vsprintf(fmt, args)); +process.exit(0); + +function dump(fmt: string): number { + if(!fmt) { console.error("printj: missing format argument"); return 1; } + // X._tokenize(fmt).forEach(function(x){console.log(x);}); + return 0; +} diff --git a/types/tsconfig.json b/types/tsconfig.json index 7a65429..5c276fe 100644 --- a/types/tsconfig.json +++ b/types/tsconfig.json @@ -4,7 +4,7 @@ "lib": [ "es5", "dom" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": ".", "paths": { "printj": ["."] }, "types": [], diff --git a/types/tslint.json b/types/tslint.json index d9401a9..a4589d3 100644 --- a/types/tslint.json +++ b/types/tslint.json @@ -1,11 +1,13 @@ { "extends": "dtslint/dtslint.json", "rules": { + "no-implicit-dependencies": false, "whitespace": false, "no-sparse-arrays": false, "only-arrow-functions": false, "no-consecutive-blank-lines": false, "prefer-conditional-expression": false, + "prefer-const": true, "one-variable-per-declaration": false } } -- 2.34.1 From e6d6f001c5d5fe6b33ea0a62cd7c907f3c2d21fb Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 29 Jul 2019 07:01:48 -0400 Subject: [PATCH 08/15] version bump 1.2.2: old node shim --- README.md | 15 ++++++------ bin/printj.njs | 1 + bits/01_version.js | 2 +- ctest/printj.js | 2 +- ctest/test.js | 1 + dist/printj.js | 56 +++++++++++++++++++-------------------------- dist/printj.min.js | 2 +- dist/printj.min.map | 2 +- lib/loop_code.mjs | 2 +- package.json | 2 +- printj.flow.js | 2 +- printj.js | 2 +- printj.mjs | 2 +- test.js | 1 + 14 files changed, 43 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 68d4e43..e9b7f40 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,15 @@ compliance, performance and IE6+ support. PRINTJ.sprintf("Hello %s!", "World"); ``` -A self-contained specification of the printf format string is included below in [this README](#printf-format-string-specification), as well as a summary of the +A self-contained specification of the printf format string is included below in +[this README](#printf-format-string-specification), as well as a summary of the [support against various printf implementations](#support-summary) ## Table of Contents
- Table of Contents (click to show) + Table of Contents (click to show) @@ -163,8 +164,6 @@ granted by the Apache 2.0 license are reserved by the Original Author. [![Dependencies Status](https://david-dm.org/sheetjs/printj/status.svg)](https://david-dm.org/sheetjs/printj) -[![ghit.me](https://ghit.me/badge.svg?repo=sheetjs/printj)](https://ghit.me/repo/sheetjs/printj) - [![Analytics](https://ga-beacon.appspot.com/UA-36810333-1/SheetJS/printj?pixel)](https://github.com/SheetJS/printj) # printf format string specification @@ -595,7 +594,7 @@ lengths exceeding 32 bits, `Math.round` is appropriate. |------|---------------------------|-------------------------------------------| | 8 | `V & 0xFF` | `V &= 0xFF; if(V > 0x7F) V-= 0x100` | | 16 | `V & 0xFFFF` | `V &= 0xFFFF; if(V > 0x7FFF) V-= 0x10000` | -| 32 | `V >>> 0` | `V | 0` | +| 32 | `V >>> 0` | `V \| 0` | | 64 | `Math.abs(Math.round(V))` | `Math.round(V)` | ## Length Specifiers for Integer Conversions @@ -900,7 +899,7 @@ In all forms other than `"%m"`, an argument will be processed as follows: - If the `errno` field is set, emit "Error number " followed by the errno - Otherwise emit "Error " followed by the error interpreted as a String -``` +```js var x = new Error("sheetjs"); x.errno = 69; x.toString = function() { return "SHEETJS"; }; printf("|%#m|", x); // |sheetjs| @@ -962,7 +961,7 @@ Width, precision and other flags are ignored. Under the "T" conversion, the result of `typeof arg` is rendered. If the `#` flag is specified, the type is derived from `Object.prototype.toString`: -``` +```js printf("%1$T %1$#T", 1); // 'number Number' printf("%1$T %1$#T", 'foo'); // 'string String' printf("%1$T %1$#T", [1,2,3]); // 'object Array' @@ -972,7 +971,7 @@ printf("%1$T %1$#T", undefined); // 'undefined Undefined' Under the "V" conversion, the result of `arg.valueOf()` is rendered: -``` +```js var _f = function() { return "f"; }; var _3 = function() { return 3; }; printf("%1$d %1$s %1$V", {toString:_f}); // '0 f f' diff --git a/bin/printj.njs b/bin/printj.njs index 53fd52f..bedb8bc 100755 --- a/bin/printj.njs +++ b/bin/printj.njs @@ -3,6 +3,7 @@ /* eslint-env node */ /* vim: set ts=2 ft=javascript: */ /*jshint node:true, evil:true */ +require("../shim"); var X = require("../"), argv = process.argv; function help() { diff --git a/bits/01_version.js b/bits/01_version.js index aafa57c..5173202 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.2.1'; +PRINTJ.version = '1.2.2'; diff --git a/ctest/printj.js b/ctest/printj.js index ff000bb..a7de1cc 100644 --- a/ctest/printj.js +++ b/ctest/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.1'; +PRINTJ.version = '1.2.2'; function tokenize(fmt) { var out = []; diff --git a/ctest/test.js b/ctest/test.js index ef30c84..c807d0e 100644 --- a/ctest/test.js +++ b/ctest/test.js @@ -3,6 +3,7 @@ var X; var IMPLS = {}, IMPLA = [], IMPL = []; if(typeof require !== 'undefined') { assert = require('assert'); + require('./shim'); X=require('./'); IMPL = require("./lib/impl.json"); IMPL.forEach(function(impl, i) { IMPLS[impl] = IMPLA[i] = require("./lib/" + impl); }); diff --git a/dist/printj.js b/dist/printj.js index a2a38d6..a7de1cc 100644 --- a/dist/printj.js +++ b/dist/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.1.2'; +PRINTJ.version = '1.2.2'; function tokenize(fmt) { var out = []; @@ -149,14 +149,6 @@ function tokenize(fmt) { return out; } -//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "") -var padstr = { - " ": " ", - "0": "000000000000000000000000000000000", - "7": "777777777777777777777777777777777", - "f": "fffffffffffffffffffffffffffffffff" -}; - /*global process:true, util:true, require:true */ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util"); var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; @@ -208,7 +200,7 @@ function doit(t, args) { /* only valid flag is "-" for left justification */ O = String(arg); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* first char of string or convert */ @@ -223,7 +215,7 @@ function doit(t, args) { case "string": O = arg.charAt(0); break; default: O = String(arg).charAt(0); } - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; /* int diDuUoOxXbB */ @@ -308,10 +300,10 @@ function doit(t, args) { /* boolean (extension) */ case /*Y*/ 89: case /*y*/ 121: - O = Boolean(arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); + O = (arg) ? (alt ? "yes" : "true") : (alt ? "no" : "false"); if(c == /*Y*/ 89) O = O.toUpperCase(); if( prec >= 0) O = O.substr(0, prec); - if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? padstr["0"].substr(0, width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? padstr[" "].substr(0, width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } + if( width > O.length || - width > O.length) { if(( flags.indexOf("-") == -1 || width < 0) && flags.indexOf("0") != -1) { pad = ( width - O.length >= 0 ? "0".repeat( width - O.length) : ""); O = pad + O; } else { pad = ( width - O.length >= 0 ? " ".repeat( width - O.length) : ""); O = flags.indexOf("-") > -1 ? O + pad : pad + O; } } break; } @@ -373,16 +365,16 @@ function doit(t, args) { if(radix == 16 || radix == -16) { O = (Vnum>>>0).toString(16); Vnum = Math.floor((Vnum - (Vnum >>> 0)) / Math.pow(2,32)); - O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? padstr[ "0"].substr(0,8 - O.length) : "") + O; - O = (16 - O.length >= 0 ? padstr[ "f"].substr(0,16 - O.length) : "") + O; + O = (Vnum>>>0).toString(16) + (8 - O.length >= 0 ? "0".repeat(8 - O.length) : "") + O; + O = (16 - O.length >= 0 ? "f".repeat(16 - O.length) : "") + O; if(radix == 16) O = O.toUpperCase(); } else if(radix == 8) { O = (Vnum>>>0).toString(8); - O = (10 - O.length >= 0 ? padstr[ "0"].substr(0,10 - O.length) : "") + O; + O = (10 - O.length >= 0 ? "0".repeat(10 - O.length) : "") + O; Vnum = Math.floor((Vnum - ((Vnum >>> 0)&0x3FFFFFFF)) / Math.pow(2,30)); O = (Vnum>>>0).toString(8) + O.substr(O.length - 10); O = O.substr(O.length - 20); - O = "1" + (21 - O.length >= 0 ? padstr[ "7"].substr(0,21 - O.length) : "") + O; + O = "1" + (21 - O.length >= 0 ? "7".repeat(21 - O.length) : "") + O; } else { Vnum = (-Vnum) % 1e16; var d1 = [1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6]; @@ -403,8 +395,8 @@ function doit(t, args) { if(prec ===0 && O == "0" && !(radix == 8 && alt)) O = ""; /* bail out */ else { if(O.length < prec + (O.substr(0,1) == "-" ? 1 : 0)) { - if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? padstr[ "0"].substr(0,prec - O.length) : "") + O; - else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? padstr[ "0"].substr(0,prec + 1 - O.length) : "") + O.substr(1); + if(O.substr(0,1) != "-") O = (prec - O.length >= 0 ? "0".repeat(prec - O.length) : "") + O; + else O = O.substr(0,1) + (prec + 1 - O.length >= 0 ? "0".repeat(prec + 1 - O.length) : "") + O.substr(1); } /* add prefix for # form */ @@ -425,10 +417,10 @@ function doit(t, args) { if(width > 0) { if(O.length < width) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && prec < 0 && O.length > 0) { - if(prec > O.length) O = ((prec - O.length) >= 0 ? padstr[ "0"].substr(0,(prec - O.length)) : "") + O; - pad = ((width - O.length) >= 0 ? padstr[ (prec > 0 ? " " : "0")].substr(0,(width - O.length)) : ""); + if(prec > O.length) O = ((prec - O.length) >= 0 ? "0".repeat((prec - O.length)) : "") + O; + pad = ((width - O.length) >= 0 ? (prec > 0 ? " " : "0").repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -436,7 +428,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } } @@ -483,8 +475,8 @@ function doit(t, args) { O = Vnum.toExponential(20); E = +O.substr(O.indexOf("e")+1); O = O.charAt(0) + O.substr(2,O.indexOf("e")-2); - O = O + (E - O.length + 1 >= 0 ? padstr[ "0"].substr(0,E - O.length + 1) : ""); - if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? padstr[ "0"].substr(0,prec) : ""); + O = O + (E - O.length + 1 >= 0 ? "0".repeat(E - O.length + 1) : ""); + if(alt || (prec > 0 && isnum !== 11)) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); break; /* e/E exponential */ @@ -498,7 +490,7 @@ function doit(t, args) { /* a/A hex */ case 4: - if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? padstr["0"].substr(0,prec) : ""):"")+"p+0"; break;} + if(Vnum===0){O= "0x0"+((alt||prec>0)?"."+(prec >= 0 ? "0".repeat(prec) : ""):"")+"p+0"; break;} O = Vnum.toString(16); /* First char 0-9 */ var ac = O.charCodeAt(0); @@ -545,11 +537,11 @@ function doit(t, args) { if(prec > 0) { O = O.substr(0, prec + 2); if(O.length < prec + 2) { - if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : "") + O.substr(1); - else O += ((prec + 2 - O.length) >= 0 ? padstr[ "0"].substr(0,(prec + 2 - O.length)) : ""); + if(O.charCodeAt(0) < 48) O = O.charAt(0) + ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : "") + O.substr(1); + else O += ((prec + 2 - O.length) >= 0 ? "0".repeat((prec + 2 - O.length)) : ""); } } else if(prec === 0) O = O.charAt(0) + (alt ? "." : ""); - } else if(prec > 0) O = O + "." + (prec >= 0 ? padstr["0"].substr(0,prec) : ""); + } else if(prec > 0) O = O + "." + (prec >= 0 ? "0".repeat(prec) : ""); else if(alt) O = O + "."; O = "0x" + O + "p" + (E>=0 ? "+" + E : E); break; @@ -566,9 +558,9 @@ function doit(t, args) { /* width */ if(width > O.length) { if(flags.indexOf("-") > -1) { - O = O + ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : ""); + O = O + ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : ""); } else if(flags.indexOf("0") > -1 && O.length > 0 && isf) { - pad = ((width - O.length) >= 0 ? padstr[ "0"].substr(0,(width - O.length)) : ""); + pad = ((width - O.length) >= 0 ? "0".repeat((width - O.length)) : ""); if(O.charCodeAt(0) < 48) { if(O.charAt(2).toLowerCase() == "x") O = O.substr(0,3) + pad + O.substring(3); else O = O.substr(0,1) + pad + O.substring(1); @@ -576,7 +568,7 @@ function doit(t, args) { else if(O.charAt(1).toLowerCase() == "x") O = O.substr(0,2) + pad + O.substring(2); else O = pad + O; } else { - O = ((width - O.length) >= 0 ? padstr[ " "].substr(0,(width - O.length)) : "") + O; + O = ((width - O.length) >= 0 ? " ".repeat((width - O.length)) : "") + O; } } if(c < 96) O = O.toUpperCase(); diff --git a/dist/printj.min.js b/dist/printj.min.js index d554866..b41a2f2 100644 --- a/dist/printj.min.js +++ b/dist/printj.min.js @@ -1,2 +1,2 @@ /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ -var PRINTJ;(function(factory){if(typeof DO_NOT_EXPORT_PRINTJ==="undefined"){if("object"===typeof exports){factory(exports)}else if("function"===typeof define&&define.amd){define(function(){var module={};factory(module);return module})}else{factory(PRINTJ={})}}else{factory(PRINTJ={})}})(function(PRINTJ){PRINTJ.version="1.1.2";function tokenize(fmt){var out=[];var start=0;var i=0;var infmt=false;var fmtparam="",fmtflags="",fmtwidth="",fmtprec="",fmtlen="";var c=0;var L=fmt.length;for(;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";O=pad+O}else{pad=width-O.length>=0?padstr[" "].substr(0,width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?padstr["0"].substr(0,8-O.length):"")+O;O=(16-O.length>=0?padstr["f"].substr(0,16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?padstr["0"].substr(0,10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?padstr["7"].substr(0,21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?padstr["0"].substr(0,prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?padstr["0"].substr(0,prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?padstr["0"].substr(0,prec-O.length):"")+O;pad=width-O.length>=0?padstr[prec>0?" ":"0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?padstr["0"].substr(0,E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?padstr["0"].substr(0,prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?padstr["0"].substr(0,prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?padstr["0"].substr(0,prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?padstr["0"].substr(0,prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?padstr["0"].substr(0,width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?padstr[" "].substr(0,width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i Date: Tue, 8 Oct 2019 02:05:51 -0400 Subject: [PATCH 09/15] License text --- LICENSE | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 189 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index de06dc2..4a5a262 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,192 @@ -Copyright (C) 2016-present SheetJS + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (C) 2016-present SheetJS LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11,4 +199,3 @@ Copyright (C) 2016-present SheetJS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - -- 2.34.1 From 850ab3846117b176c700434a8c0db5b2353334ae Mon Sep 17 00:00:00 2001 From: gregod Date: Sat, 6 Feb 2021 18:23:37 +0000 Subject: [PATCH 10/15] Fix typo in module path in package.json Otherwise tools like snowpack that rely on the module attribute fail. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f1f842f..7f35fdb 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "printj": "./bin/printj.njs" }, "main": "./printj", - "module": "./print.mjs", + "module": "./printj.mjs", "types": "types", "browser": { "process": false, -- 2.34.1 From 6fb0427a3163312811e3ca9ea023d773352801bc Mon Sep 17 00:00:00 2001 From: SheetJS Date: Sun, 29 Aug 2021 22:31:43 -0400 Subject: [PATCH 11/15] version bump 1.2.3 --- bits/01_version.js | 2 +- dist/LICENSE | 191 ++++++++++++++++++++++++++++++++++++++++++++- dist/printj.js | 2 +- dist/printj.min.js | 2 +- lib/loop_code.mjs | 2 +- package.json | 2 +- printj.flow.js | 2 +- printj.js | 2 +- printj.mjs | 2 +- 9 files changed, 197 insertions(+), 10 deletions(-) diff --git a/bits/01_version.js b/bits/01_version.js index 5173202..692e487 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.2.2'; +PRINTJ.version = '1.2.3'; diff --git a/dist/LICENSE b/dist/LICENSE index de06dc2..4a5a262 100644 --- a/dist/LICENSE +++ b/dist/LICENSE @@ -1,4 +1,192 @@ -Copyright (C) 2016-present SheetJS + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (C) 2016-present SheetJS LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11,4 +199,3 @@ Copyright (C) 2016-present SheetJS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - diff --git a/dist/printj.js b/dist/printj.js index a7de1cc..778d0bb 100644 --- a/dist/printj.js +++ b/dist/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.2'; +PRINTJ.version = '1.2.3'; function tokenize(fmt) { var out = []; diff --git a/dist/printj.min.js b/dist/printj.min.js index b41a2f2..00e7cfd 100644 --- a/dist/printj.min.js +++ b/dist/printj.min.js @@ -1,2 +1,2 @@ /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ -var PRINTJ;(function(factory){if(typeof DO_NOT_EXPORT_PRINTJ==="undefined"){if("object"===typeof exports){factory(exports)}else if("function"===typeof define&&define.amd){define(function(){var module={};factory(module);return module})}else{factory(PRINTJ={})}}else{factory(PRINTJ={})}})(function(PRINTJ){PRINTJ.version="1.2.2";function tokenize(fmt){var out=[];var start=0;var i=0;var infmt=false;var fmtparam="",fmtflags="",fmtwidth="",fmtprec="",fmtlen="";var c=0;var L=fmt.length;for(;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i Date: Mon, 30 Aug 2021 00:50:54 -0400 Subject: [PATCH 12/15] README update [ci skip] --- README.md | 39 ++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e9b7f40..41286bb 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ A self-contained specification of the printf format string is included below in * [Installation](#installation) + + [ES Module Support](#es-module-support) * [Usage](#usage) * [Testing](#testing) * [License](#license) @@ -95,6 +96,38 @@ string with the given arguments. Running the script with `-h` displays help. The script will manipulate `module.exports` if available. This is not always desirable. To prevent the behavior, define `DO_NOT_EXPORT_PRINTJ` +### ES Module Support + +The bundle ships with a `printj.mjs` script that acts as an ES Module. + +**NodeJS** + +NodeJS 12+ support ES modules. The default import uses the CommonJS script: + +```js +import PRINTJ from "printj"; +``` + +It is possible to use the ESM powered script referencing `printj.mjs` directly: + +```js +import * as PRINTJ from "printj/printj.mjs"; // pull all exports +import { sprintf } from "printj/printj.mjs"; // pull `sprintf` +``` + +**Browser Module Support** + +Chrome 61+ and Safari 11+ support module imports in the web browser. The `.mjs` +script can be imported from a `script type=module` block: + +```html + +``` + + ## Usage In all cases, the relevant function takes a format and arguments to be rendered. @@ -693,10 +726,10 @@ This implementation performs the required adjustments. Aside from the special cases discussed above, JS `num.toExponential(prec)` differs from POSIX `printf("%1$.*2$e", num, prec)` in the exponent field: JS writes exponents with the fewest digits (POSIX requires 2+ digits). This is -easily fixed by inspecting the output string and inserting a "0" when needed. +addressed by inspecting the output string and inserting a "0" when needed. The optional `#` flag forces the decimal point to appear when precision is 0. -This is also easily corrected by adding a decimal point just before the "e". +This is also fixed by adding a decimal point just before the "e". ## Standard Form ("f" and "F" conversions) @@ -749,7 +782,7 @@ JS `num.toString(radix)` is implementation-dependent for radices other than 10 (`2-9, 11-36`). IE uses hex-mantissa decimal-hex-exponent form when the absolute value of the base-2 exponent exceeds 60. Otherwise, IE uses an exact standard hexadecimal form. Chrome, Safari and other browsers always use the -exact standard hexadecimal form. Both forms are easily converted to `"%a"` by +exact standard hexadecimal form. Both forms are converted to `"%a"` by calculating and dividing by the appropriate power of 2. For each non-zero normal floating point value, there are 4 acceptable strings diff --git a/package.json b/package.json index dd031a4..fa02946 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ }, "alex": { "allow": [ - "period" + "period", "just", "special", "invalid" ] }, "homepage": "http://sheetjs.com/opensource", -- 2.34.1 From 7847c20022b4f84507e5603a16374a09196e8972 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 30 Aug 2021 17:46:24 -0400 Subject: [PATCH 13/15] version bump 1.3.0: memoization --- bits/01_version.js | 2 +- bits/10_tokenize.js | 7 +++++-- bits/50_doit.js | 21 +++++++++++---------- bits/54_convmisc.js | 3 ++- ctest/printj.js | 33 +++++++++++++++++++-------------- dist/printj.js | 33 +++++++++++++++++++-------------- dist/printj.min.js | 2 +- dist/printj.min.map | 2 +- lib/loop_code.mjs | 33 +++++++++++++++++++-------------- package.json | 15 +++++++++------ printj.flow.js | 33 +++++++++++++++++++-------------- printj.js | 33 +++++++++++++++++++-------------- printj.mjs | 33 +++++++++++++++++++-------------- 13 files changed, 144 insertions(+), 106 deletions(-) diff --git a/bits/01_version.js b/bits/01_version.js index 692e487..dac9450 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.2.3'; +PRINTJ.version = '1.3.0'; diff --git a/bits/10_tokenize.js b/bits/10_tokenize.js index 0a5277b..2a4ca74 100644 --- a/bits/10_tokenize.js +++ b/bits/10_tokenize.js @@ -4,8 +4,11 @@ #define GETCHAR c = fmt.charCodeAt(i); #endif +var tcache = {}; + #define DRAIN(idx) if(start < idx) out.push(["L", fmt.substring(start, idx)]); function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { + if(tcache[fmt]) return tcache[fmt]; var out/*:ParsedFmt*/ = []; var start/*:number*/ = 0; @@ -16,7 +19,7 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { DRAIN(m.index) start = m.index + m[0].length; if(m[0] === "%%") out.push(["%","%"]); - else out.push([m[6], m[0], m[1], m[2], m[3], m[4], m[5]]); + else out.push([m[6], m[0]||"", m[1]||"", m[2]||"", m[3]||"", m[4]||"", m[5]||""]); } #else /* NOT USE_REGEX */ @@ -74,5 +77,5 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { #endif /* USE_REGEX */ if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } diff --git a/bits/50_doit.js b/bits/50_doit.js index 408cd03..229355b 100644 --- a/bits/50_doit.js +++ b/bits/50_doit.js @@ -10,7 +10,8 @@ var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect #endif function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { - var o/*:Array*/ = []; + //var o/*:Array*/ = []; + var o = ""; var argidx/*:number*/ = 0, idx/*:number*/ = 0; var Vnum/*:number*/ = 0; var pad/*:string*/ = ""; @@ -18,24 +19,24 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O/*:string*/ = ""; var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = SIZEOF_INT, sign/*:boolean*/ = false; /* flags */ - var flags/*:string*/ = m[IDX_FLAGS]||""; + var flags/*:string*/ = m[IDX_FLAGS]/*||""*/; var alt/*:boolean*/ = flags.indexOf("#") > -1; /* position */ - if(m[IDX_POS]) argidx = parseInt(m[IDX_POS])-1; + if(m[IDX_POS]) argidx = parseInt(m[IDX_POS], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } #define GRAB_INT(NAME, IDX, DFL) \ var NAME/*:number*/ = DFL; \ - if(m[IDX] != null && m[IDX].length > 0) { \ + if(/*m[IDX] != null &&*/ m[IDX].length > 0) { \ if(m[IDX].charAt(0) !== '*') NAME = parseInt(m[IDX], 10); \ else if(m[IDX].length === 1) NAME = args[idx++]; \ else NAME = args[parseInt(m[IDX].substr(1), 10)-1]; \ @@ -54,7 +55,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var arg/*:any*/ = args[argidx]; /* grab length */ - var len/*:string*/ = m[IDX_LEN] || ""; + var len/*:string*/ = m[IDX_LEN]/* || ""*/; #define INT isnum = -1 #define SGN(s) sign = s @@ -81,7 +82,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { #include "70_float.js" } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } diff --git a/bits/54_convmisc.js b/bits/54_convmisc.js index a2fa44a..b77498d 100644 --- a/bits/54_convmisc.js +++ b/bits/54_convmisc.js @@ -11,7 +11,8 @@ /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ diff --git a/ctest/printj.js b/ctest/printj.js index a7de1cc..09bd070 100644 --- a/ctest/printj.js +++ b/ctest/printj.js @@ -25,9 +25,12 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.2'; +PRINTJ.version = '1.3.0'; + +var tcache = {}; function tokenize(fmt) { + if(tcache[fmt]) return tcache[fmt]; var out = []; var start = 0; @@ -146,7 +149,7 @@ function tokenize(fmt) { } if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } /*global process:true, util:true, require:true */ @@ -154,7 +157,8 @@ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.no var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; function doit(t, args) { - var o = []; + //var o = []; + var o = ""; var argidx = 0, idx = 0; var Vnum = 0; var pad = ""; @@ -162,26 +166,26 @@ function doit(t, args) { var m = t[i], c = (m[0]).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O = ""; var isnum = 0, radix = 10, bytes = 4, sign = false; /* flags */ - var flags = m[3]||""; + var flags = m[3]/*||""*/; var alt = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -190,7 +194,7 @@ function doit(t, args) { var arg = args[argidx]; /* grab length */ - var len = m[6] || ""; + var len = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -276,7 +280,8 @@ function doit(t, args) { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -575,9 +580,9 @@ function doit(t, args) { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt, args) { return doit(tokenize(fmt), args); } diff --git a/dist/printj.js b/dist/printj.js index 778d0bb..09bd070 100644 --- a/dist/printj.js +++ b/dist/printj.js @@ -25,9 +25,12 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.3'; +PRINTJ.version = '1.3.0'; + +var tcache = {}; function tokenize(fmt) { + if(tcache[fmt]) return tcache[fmt]; var out = []; var start = 0; @@ -146,7 +149,7 @@ function tokenize(fmt) { } if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } /*global process:true, util:true, require:true */ @@ -154,7 +157,8 @@ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.no var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; function doit(t, args) { - var o = []; + //var o = []; + var o = ""; var argidx = 0, idx = 0; var Vnum = 0; var pad = ""; @@ -162,26 +166,26 @@ function doit(t, args) { var m = t[i], c = (m[0]).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O = ""; var isnum = 0, radix = 10, bytes = 4, sign = false; /* flags */ - var flags = m[3]||""; + var flags = m[3]/*||""*/; var alt = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -190,7 +194,7 @@ function doit(t, args) { var arg = args[argidx]; /* grab length */ - var len = m[6] || ""; + var len = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -276,7 +280,8 @@ function doit(t, args) { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -575,9 +580,9 @@ function doit(t, args) { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt, args) { return doit(tokenize(fmt), args); } diff --git a/dist/printj.min.js b/dist/printj.min.js index 00e7cfd..43d92ba 100644 --- a/dist/printj.min.js +++ b/dist/printj.min.js @@ -1,2 +1,2 @@ /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ -var PRINTJ;(function(factory){if(typeof DO_NOT_EXPORT_PRINTJ==="undefined"){if("object"===typeof exports){factory(exports)}else if("function"===typeof define&&define.amd){define(function(){var module={};factory(module);return module})}else{factory(PRINTJ={})}}else{factory(PRINTJ={})}})(function(PRINTJ){PRINTJ.version="1.2.3";function tokenize(fmt){var out=[];var start=0;var i=0;var infmt=false;var fmtparam="",fmtflags="",fmtwidth="",fmtprec="",fmtlen="";var c=0;var L=fmt.length;for(;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2])-1;else if(c===109&&!alt){o.push("Success");continue}var width=0;if(m[4]!=null&&m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5]!=null&&m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6]||"";switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=0;for(var oo=0;oo=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o.push(O)}return o.join("")}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2],10)-1;else if(c===109&&!alt){o+="Success";continue}var width=0;if(m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6];switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=o.length}continue;case 109:if(!(arg instanceof Error))O="Success";else if(arg.message)O=arg.message;else if(arg.errno)O="Error number "+arg.errno;else O="Error "+String(arg);break;case 74:O=(alt?u_inspect:JSON.stringify)(arg);break;case 86:O=arg==null?"null":String(arg.valueOf());break;case 84:if(alt){O=Object.prototype.toString.call(arg).substr(8);O=O.substr(0,O.length-1)}else O=typeof arg;break;case 89:;case 121:O=arg?alt?"yes":"true":alt?"no":"false";if(c==89)O=O.toUpperCase();if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o+=O}return o}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;istring*/ = JSON.stringify; function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { - var o/*:Array*/ = []; + //var o/*:Array*/ = []; + var o = ""; var argidx/*:number*/ = 0, idx/*:number*/ = 0; var Vnum/*:number*/ = 0; var pad/*:string*/ = ""; @@ -144,26 +148,26 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O/*:string*/ = ""; var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false; /* flags */ - var flags/*:string*/ = m[3]||""; + var flags/*:string*/ = m[3]/*||""*/; var alt/*:boolean*/ = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -172,7 +176,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var arg/*:any*/ = args[argidx]; /* grab length */ - var len/*:string*/ = m[6] || ""; + var len/*:string*/ = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -258,7 +262,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -557,9 +562,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); } diff --git a/package.json b/package.json index fa02946..cc2a481 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "printj", - "version": "1.2.3", + "version": "1.3.0", "author": "sheetjs", "description": "Pure-JS printf", "keywords": [ @@ -22,11 +22,11 @@ "dependencies": { }, "devDependencies": { - "mocha": "~2.5.3", - "blanket": "~1.2.3", "@sheetjs/uglify-js": "~2.7.3", "@types/node": "^8.0.7", + "blanket": "~1.2.3", "dtslint": "^0.1.2", + "mocha": "~2.5.3", "typescript": "2.2.0" }, "repository": { @@ -46,11 +46,14 @@ }, "alex": { "allow": [ - "period", "just", "special", "invalid" + "special", + "period", + "just", + "invalid" ] }, - "homepage": "http://sheetjs.com/opensource", - "files": ["printj.js", "printj.mjs", "bin/printj.njs", "LICENSE", "README.md", "dist/*.js", "dist/*.map", "dist/LICENSE", "types/index.d.ts", "types/*.json"], + "homepage": "https://sheetjs.com/", + "files": ["printj.js", "printj.mjs", "bin/printj.njs", "LICENSE", "README.md", "shim.js", "dist/*.js", "dist/*.map", "dist/LICENSE", "types/index.d.ts", "types/*.json"], "bugs": { "url": "https://github.com/SheetJS/printj/issues" }, diff --git a/printj.flow.js b/printj.flow.js index 72b367c..68358d2 100644 --- a/printj.flow.js +++ b/printj.flow.js @@ -28,9 +28,12 @@ var PRINTJ/*:PRINTJModule*/; /*jshint ignore:end */ }(function(PRINTJ/*:PRINTJModule*/) { -PRINTJ.version = '1.2.3'; +PRINTJ.version = '1.3.0'; + +var tcache = {}; function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { + if(tcache[fmt]) return tcache[fmt]; var out/*:ParsedFmt*/ = []; var start/*:number*/ = 0; @@ -149,7 +152,7 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { } if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } /*:: var util = require('util'); */ @@ -158,7 +161,8 @@ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.no var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect : JSON.stringify; function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { - var o/*:Array*/ = []; + //var o/*:Array*/ = []; + var o = ""; var argidx/*:number*/ = 0, idx/*:number*/ = 0; var Vnum/*:number*/ = 0; var pad/*:string*/ = ""; @@ -166,26 +170,26 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O/*:string*/ = ""; var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false; /* flags */ - var flags/*:string*/ = m[3]||""; + var flags/*:string*/ = m[3]/*||""*/; var alt/*:boolean*/ = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -194,7 +198,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var arg/*:any*/ = args[argidx]; /* grab length */ - var len/*:string*/ = m[6] || ""; + var len/*:string*/ = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -280,7 +284,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -579,9 +584,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); } diff --git a/printj.js b/printj.js index 778d0bb..09bd070 100644 --- a/printj.js +++ b/printj.js @@ -25,9 +25,12 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.2.3'; +PRINTJ.version = '1.3.0'; + +var tcache = {}; function tokenize(fmt) { + if(tcache[fmt]) return tcache[fmt]; var out = []; var start = 0; @@ -146,7 +149,7 @@ function tokenize(fmt) { } if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } /*global process:true, util:true, require:true */ @@ -154,7 +157,8 @@ if(typeof process !== 'undefined' && !!process.versions && !!process.versions.no var u_inspect = (typeof util != 'undefined') ? util.inspect : JSON.stringify; function doit(t, args) { - var o = []; + //var o = []; + var o = ""; var argidx = 0, idx = 0; var Vnum = 0; var pad = ""; @@ -162,26 +166,26 @@ function doit(t, args) { var m = t[i], c = (m[0]).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O = ""; var isnum = 0, radix = 10, bytes = 4, sign = false; /* flags */ - var flags = m[3]||""; + var flags = m[3]/*||""*/; var alt = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -190,7 +194,7 @@ function doit(t, args) { var arg = args[argidx]; /* grab length */ - var len = m[6] || ""; + var len = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -276,7 +280,8 @@ function doit(t, args) { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -575,9 +580,9 @@ function doit(t, args) { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt, args) { return doit(tokenize(fmt), args); } diff --git a/printj.mjs b/printj.mjs index 12d1c62..109cd30 100644 --- a/printj.mjs +++ b/printj.mjs @@ -7,11 +7,14 @@ var PRINTJ/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/; -PRINTJ.version = '1.2.3'; +PRINTJ.version = '1.3.0'; export const version = PRINTJ.version; +var tcache = {}; + function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { + if(tcache[fmt]) return tcache[fmt]; var out/*:ParsedFmt*/ = []; var start/*:number*/ = 0; @@ -130,13 +133,14 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ { } if(start < fmt.length) out.push(["L", fmt.substring(start)]); - return out; + return (tcache[fmt] = out); } var u_inspect/*:(o:any)=>string*/ = JSON.stringify; function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { - var o/*:Array*/ = []; + //var o/*:Array*/ = []; + var o = ""; var argidx/*:number*/ = 0, idx/*:number*/ = 0; var Vnum/*:number*/ = 0; var pad/*:string*/ = ""; @@ -144,26 +148,26 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var m/*:ParsedEntry*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0); /* m order: conv full param flags width prec length */ - if(c === /*L*/ 76) { o.push(m[1]); continue; } - if(c === /*%*/ 37) { o.push("%"); continue; } + if(c === /*L*/ 76) { o += /*o.push*/(m[1]); continue; } + if(c === /*%*/ 37) { o += /*o.push*/("%"); continue; } var O/*:string*/ = ""; var isnum/*:number*/ = 0, radix/*:number*/ = 10, bytes/*:number*/ = 4, sign/*:boolean*/ = false; /* flags */ - var flags/*:string*/ = m[3]||""; + var flags/*:string*/ = m[3]/*||""*/; var alt/*:boolean*/ = flags.indexOf("#") > -1; /* position */ - if(m[2]) argidx = parseInt(m[2])-1; + if(m[2]) argidx = parseInt(m[2], 10)-1; /* %m special case */ - else if(c === /*m*/ 109 && !alt) { o.push("Success"); continue; } + else if(c === /*m*/ 109 && !alt) { o += /*.push*/("Success"); continue; } /* grab width */ - var width = 0; if(m[ 4] != null && m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } + var width = 0; if( m[ 4].length > 0) { if(m[ 4].charAt(0) !== '*') width = parseInt(m[ 4], 10); else if(m[ 4].length === 1) width = args[idx++]; else width = args[parseInt(m[ 4].substr(1), 10)-1]; } /* grab precision */ - var prec = -1; if(m[ 5] != null && m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } + var prec = -1; if( m[ 5].length > 0) { if(m[ 5].charAt(0) !== '*') prec = parseInt(m[ 5], 10); else if(m[ 5].length === 1) prec = args[idx++]; else prec = args[parseInt(m[ 5].substr(1), 10)-1]; } /* position not specified */ if(!m[2]) argidx = idx++; @@ -172,7 +176,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { var arg/*:any*/ = args[argidx]; /* grab length */ - var len/*:string*/ = m[6] || ""; + var len/*:string*/ = m[6]/* || ""*/; switch(c) { /* str cCsS */ @@ -258,7 +262,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { /* store length in the `len` key */ case /*n*/ 110: - if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } + if(arg) { arg.len = o.length; } + //if(arg) { arg.len=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; } continue; /* process error */ @@ -557,9 +562,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array*/)/*:string*/ { } - o.push(O); + o += /*.push*/(O); } - return o.join(""); + return o/*.join("")*/; } function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); } -- 2.34.1 From d8b2f48bd97c43049102209ba86b693b824a7f12 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Sun, 23 Jan 2022 23:33:44 -0500 Subject: [PATCH 14/15] version bump 1.3.1: npm minutiae --- Makefile | 37 ++++++++++++++++++++++++------------- bin/printj.njs | 5 ++--- bits/01_version.js | 2 +- dist/printj.js | 2 +- dist/printj.min.js | 2 +- lib/loop_code.mjs | 2 +- package.json | 21 ++++++++++++++------- printj.flow.js | 2 +- printj.js | 2 +- printj.mjs | 2 +- types/bin_printj.ts | 2 +- types/index.d.ts | 1 - types/tsconfig.json | 1 + 13 files changed, 49 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 983d406..d3e7d23 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,13 @@ FLOWTARGET=$(LIB).flow.js MJSTARGET=$(LIB).mjs FLOWTGTS=$(TARGET) $(AUXTARGETS) CLOSURE=/usr/local/lib/node_modules/google-closure-compiler/compiler.jar +UGLIFY="./node_modules/@sheetjs/uglify-js/bin/uglifyjs" +JSCS=./node_modules/.bin/jscs +JSHINT=./node_modules/.bin/jshint +MOCHA=./node_modules/.bin/mocha +ESLINT=./node_modules/.bin/eslint +ALEX=./node_modules/.bin/alex +MDSPELL=./node_modules/.bin/mdspell ## Main Targets @@ -37,18 +44,22 @@ clean: clean-stress ## Remove targets and build artifacts @OUTDIR=$(PWD)/lib make -C bits clean rm -f $(TARGET) $(FLOWTARGET) +.PHONY: init +init: ## Initial setup for development + npm i + .PHONY: dist dist: $(TARGET) ## Prepare JS files for distribution cp $(TARGET) dist/ cp LICENSE dist/ - uglifyjs $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)" + $(UGLIFY) $(TARGET) -o dist/$(LIB).min.js --source-map dist/$(LIB).min.map --preamble "$$(head -n 1 bits/00_header.js)" misc/strip_sourcemap.sh dist/$(LIB).min.js ## Testing .PHONY: test mocha test mocha: test.js $(TARGET) ## Run test suite - mocha -R spec -t 20000 + $(MOCHA) -R spec -t 20000 .PHONY: ctest ctest: ## Build browser test (into ctest/ subdirectory) @@ -73,20 +84,20 @@ clean-stress: ## Remove stress tests ## Code Checking .PHONY: fullint -fullint: lint old-lint tslint flow mdlint ## Run all checks +fullint: lint old-lint tslint mdlint ## Run all checks .PHONY: lint lint: $(TARGET) ## Run eslint checks - @eslint --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json + @$(ESLINT) --ext .js,.njs,.json,.html,.htm $(TARGET) $(AUXTARGETS) $(CMDS) $(HTMLLINT) package.json if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi .PHONY: old-lint old-lint: $(TARGET) ## Run jshint and jscs checks - @jshint --show-non-errors $(TARGET) $(AUXTARGETS) - @jshint --show-non-errors $(CMDS) - @jshint --show-non-errors package.json - @jshint --show-non-errors --extract=always $(HTMLLINT) - @jscs lib/*.js + @$(JSHINT) --show-non-errors $(TARGET) $(AUXTARGETS) + @$(JSHINT) --show-non-errors $(CMDS) + @$(JSHINT) --show-non-errors package.json + @$(JSHINT) --show-non-errors --extract=always $(HTMLLINT) + @$(JSCS) lib/*.js if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi .PHONY: tslint @@ -102,17 +113,17 @@ flow: lint ## Run flow checker cov: misc/coverage.html ## Run coverage test misc/coverage.html: $(TARGET) test.js - mocha --require blanket -R html-cov -t 20000 > $@ + $(MOCHA) --require blanket -R html-cov -t 20000 > $@ .PHONY: coveralls coveralls: ## Coverage Test + Send to coveralls.io - mocha --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js + $(MOCHA) --require blanket --reporter mocha-lcov-reporter -t 20000 | node ./node_modules/coveralls/bin/coveralls.js MDLINT=README.md .PHONY: mdlint mdlint: $(MDLINT) ## Check markdown documents - alex $^ - mdspell -a -n -x -r --en-us $^ + $(ALEX) $^ + $(MDSPELL) -a -n -x -r --en-us $^ .PHONY: help help: diff --git a/bin/printj.njs b/bin/printj.njs index bedb8bc..5e1ae8d 100755 --- a/bin/printj.njs +++ b/bin/printj.njs @@ -1,5 +1,5 @@ #!/usr/bin/env node -/* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ +/* printj.js (C) 2016-present SheetJS -- https://sheetjs.com */ /* eslint-env node */ /* vim: set ts=2 ft=javascript: */ /*jshint node:true, evil:true */ @@ -28,8 +28,7 @@ function help() { " $ printj '|%s is %s|' s:1+2 e:1+2 # |1+2 is 3|", " $ printj '|%c %c|' s:69 n:69 # |6 E|", "", -"Support email: dev@sheetjs.com", -"Web Demo: http://oss.sheetjs.com/printj/" +"Web Demo: https://oss.sheetjs.com/printj/" ].forEach(function(l) { console.log(l); }); return 0; } diff --git a/bits/01_version.js b/bits/01_version.js index dac9450..c39cf20 100644 --- a/bits/01_version.js +++ b/bits/01_version.js @@ -1 +1 @@ -PRINTJ.version = '1.3.0'; +PRINTJ.version = '1.3.1'; diff --git a/dist/printj.js b/dist/printj.js index 09bd070..a102fa2 100644 --- a/dist/printj.js +++ b/dist/printj.js @@ -25,7 +25,7 @@ var PRINTJ; /*jshint ignore:end */ }(function(PRINTJ) { -PRINTJ.version = '1.3.0'; +PRINTJ.version = '1.3.1'; var tcache = {}; diff --git a/dist/printj.min.js b/dist/printj.min.js index 43d92ba..b43f1b7 100644 --- a/dist/printj.min.js +++ b/dist/printj.min.js @@ -1,2 +1,2 @@ /* printj.js (C) 2016-present SheetJS -- http://sheetjs.com */ -var PRINTJ;(function(factory){if(typeof DO_NOT_EXPORT_PRINTJ==="undefined"){if("object"===typeof exports){factory(exports)}else if("function"===typeof define&&define.amd){define(function(){var module={};factory(module);return module})}else{factory(PRINTJ={})}}else{factory(PRINTJ={})}})(function(PRINTJ){PRINTJ.version="1.3.0";var tcache={};function tokenize(fmt){if(tcache[fmt])return tcache[fmt];var out=[];var start=0;var i=0;var infmt=false;var fmtparam="",fmtflags="",fmtwidth="",fmtprec="",fmtlen="";var c=0;var L=fmt.length;for(;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2],10)-1;else if(c===109&&!alt){o+="Success";continue}var width=0;if(m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6];switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=o.length}continue;case 109:if(!(arg instanceof Error))O="Success";else if(arg.message)O=arg.message;else if(arg.errno)O="Error number "+arg.errno;else O="Error "+String(arg);break;case 74:O=(alt?u_inspect:JSON.stringify)(arg);break;case 86:O=arg==null?"null":String(arg.valueOf());break;case 84:if(alt){O=Object.prototype.toString.call(arg).substr(8);O=O.substr(0,O.length-1)}else O=typeof arg;break;case 89:;case 121:O=arg?alt?"yes":"true":alt?"no":"false";if(c==89)O=O.toUpperCase();if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o+=O}return o}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i=48&&c<58){if(fmtprec.length)fmtprec+=String.fromCharCode(c);else if(c==48&&!fmtwidth.length)fmtflags+=String.fromCharCode(c);else fmtwidth+=String.fromCharCode(c)}else switch(c){case 36:if(fmtprec.length)fmtprec+="$";else if(fmtwidth.charAt(0)=="*")fmtwidth+="$";else{fmtparam=fmtwidth+"$";fmtwidth=""}break;case 39:fmtflags+="'";break;case 45:fmtflags+="-";break;case 43:fmtflags+="+";break;case 32:fmtflags+=" ";break;case 35:fmtflags+="#";break;case 46:fmtprec=".";break;case 42:if(fmtprec.charAt(0)==".")fmtprec+="*";else fmtwidth+="*";break;case 104:;case 108:if(fmtlen.length>1)throw"bad length "+fmtlen+String(c);fmtlen+=String.fromCharCode(c);break;case 76:;case 106:;case 122:;case 116:;case 113:;case 90:;case 119:if(fmtlen!=="")throw"bad length "+fmtlen+String.fromCharCode(c);fmtlen=String.fromCharCode(c);break;case 73:if(fmtlen!=="")throw"bad length "+fmtlen+"I";fmtlen="I";break;case 100:;case 105:;case 111:;case 117:;case 120:;case 88:;case 102:;case 70:;case 101:;case 69:;case 103:;case 71:;case 97:;case 65:;case 99:;case 67:;case 115:;case 83:;case 112:;case 110:;case 68:;case 85:;case 79:;case 109:;case 98:;case 66:;case 121:;case 89:;case 74:;case 86:;case 84:;case 37:infmt=false;if(fmtprec.length>1)fmtprec=fmtprec.substr(1);out.push([String.fromCharCode(c),fmt.substring(start,i+1),fmtparam,fmtflags,fmtwidth,fmtprec,fmtlen]);start=i+1;fmtlen=fmtprec=fmtwidth=fmtflags=fmtparam="";break;default:throw new Error("Invalid format string starting with |"+fmt.substring(start,i+1)+"|");}}if(start-1;if(m[2])argidx=parseInt(m[2],10)-1;else if(c===109&&!alt){o+="Success";continue}var width=0;if(m[4].length>0){if(m[4].charAt(0)!=="*")width=parseInt(m[4],10);else if(m[4].length===1)width=args[idx++];else width=args[parseInt(m[4].substr(1),10)-1]}var prec=-1;if(m[5].length>0){if(m[5].charAt(0)!=="*")prec=parseInt(m[5],10);else if(m[5].length===1)prec=args[idx++];else prec=args[parseInt(m[5].substr(1),10)-1]}if(!m[2])argidx=idx++;var arg=args[argidx];var len=m[6];switch(c){case 83:;case 115:O=String(arg);if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 67:;case 99:switch(typeof arg){case"number":var cc=arg;if(c==67||len.charCodeAt(0)===108){cc&=4294967295;O=String.fromCharCode(cc)}else{cc&=255;O=String.fromCharCode(cc)}break;case"string":O=arg.charAt(0);break;default:O=String(arg).charAt(0);}if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;case 68:bytes=8;case 100:;case 105:isnum=-1;sign=true;break;case 85:bytes=8;case 117:isnum=-1;break;case 79:bytes=8;case 111:isnum=-1;radix=8;break;case 120:isnum=-1;radix=-16;break;case 88:isnum=-1;radix=16;break;case 66:bytes=8;case 98:isnum=-1;radix=2;break;case 70:;case 102:isnum=1;break;case 69:;case 101:isnum=2;break;case 71:;case 103:isnum=3;break;case 65:;case 97:isnum=4;break;case 112:Vnum=typeof arg=="number"?arg:arg?Number(arg.l):-1;if(isNaN(Vnum))Vnum=-1;if(alt)O=Vnum.toString(10);else{Vnum=Math.abs(Vnum);O="0x"+Vnum.toString(16).toLowerCase()}break;case 110:if(arg){arg.len=o.length}continue;case 109:if(!(arg instanceof Error))O="Success";else if(arg.message)O=arg.message;else if(arg.errno)O="Error number "+arg.errno;else O="Error "+String(arg);break;case 74:O=(alt?u_inspect:JSON.stringify)(arg);break;case 86:O=arg==null?"null":String(arg.valueOf());break;case 84:if(alt){O=Object.prototype.toString.call(arg).substr(8);O=O.substr(0,O.length-1)}else O=typeof arg;break;case 89:;case 121:O=arg?alt?"yes":"true":alt?"no":"false";if(c==89)O=O.toUpperCase();if(prec>=0)O=O.substr(0,prec);if(width>O.length||-width>O.length){if((flags.indexOf("-")==-1||width<0)&&flags.indexOf("0")!=-1){pad=width-O.length>=0?"0".repeat(width-O.length):"";O=pad+O}else{pad=width-O.length>=0?" ".repeat(width-O.length):"";O=flags.indexOf("-")>-1?O+pad:pad+O}}break;}if(width<0){width=-width;flags+="-"}if(isnum==-1){Vnum=Number(arg);switch(len){case"hh":{bytes=1}break;case"h":{bytes=2}break;case"l":{if(bytes==4)bytes=8}break;case"L":;case"q":;case"ll":{if(bytes==4)bytes=8}break;case"j":{if(bytes==4)bytes=8}break;case"t":{if(bytes==4)bytes=8}break;case"z":;case"Z":{if(bytes==4)bytes=8}break;case"I":{if(bytes==4)bytes=8}break;case"w":break;}switch(bytes){case 1:Vnum=Vnum&255;if(sign&&Vnum>127)Vnum-=255+1;break;case 2:Vnum=Vnum&65535;if(sign&&Vnum>32767)Vnum-=65535+1;break;case 4:Vnum=sign?Vnum|0:Vnum>>>0;break;default:Vnum=isNaN(Vnum)?0:Math.round(Vnum);break;}if(bytes>4&&Vnum<0&&!sign){if(radix==16||radix==-16){O=(Vnum>>>0).toString(16);Vnum=Math.floor((Vnum-(Vnum>>>0))/Math.pow(2,32));O=(Vnum>>>0).toString(16)+(8-O.length>=0?"0".repeat(8-O.length):"")+O;O=(16-O.length>=0?"f".repeat(16-O.length):"")+O;if(radix==16)O=O.toUpperCase()}else if(radix==8){O=(Vnum>>>0).toString(8);O=(10-O.length>=0?"0".repeat(10-O.length):"")+O;Vnum=Math.floor((Vnum-(Vnum>>>0&1073741823))/Math.pow(2,30));O=(Vnum>>>0).toString(8)+O.substr(O.length-10);O=O.substr(O.length-20);O="1"+(21-O.length>=0?"7".repeat(21-O.length):"")+O}else{Vnum=-Vnum%1e16;var d1=[1,8,4,4,6,7,4,4,0,7,3,7,0,9,5,5,1,6,1,6];var di=d1.length-1;while(Vnum>0){if((d1[di]-=Vnum%10)<0){d1[di]+=10;d1[di-1]--}--di;Vnum=Math.floor(Vnum/10)}O=d1.join("")}}else{if(radix===-16)O=Vnum.toString(16).toLowerCase();else if(radix===16)O=Vnum.toString(16).toUpperCase();else O=Vnum.toString(radix)}if(prec===0&&O=="0"&&!(radix==8&&alt))O="";else{if(O.length=0?"0".repeat(prec-O.length):"")+O;else O=O.substr(0,1)+(prec+1-O.length>=0?"0".repeat(prec+1-O.length):"")+O.substr(1)}if(!sign&&alt&&Vnum!==0)switch(radix){case-16:O="0x"+O;break;case 16:O="0X"+O;break;case 8:if(O.charAt(0)!="0")O="0"+O;break;case 2:O="0b"+O;break;}}if(sign&&O.charAt(0)!="-"){if(flags.indexOf("+")>-1)O="+"+O;else if(flags.indexOf(" ")>-1)O=" "+O}if(width>0){if(O.length-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&prec<0&&O.length>0){if(prec>O.length)O=(prec-O.length>=0?"0".repeat(prec-O.length):"")+O;pad=width-O.length>=0?(prec>0?" ":"0").repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}}}else if(isnum>0){Vnum=Number(arg);if(arg===null)Vnum=0/0;if(len=="L")bytes=12;var isf=isFinite(Vnum);if(!isf){if(Vnum<0)O="-";else if(flags.indexOf("+")>-1)O="+";else if(flags.indexOf(" ")>-1)O=" ";O+=isNaN(Vnum)?"nan":"inf"}else{var E=0;if(prec==-1&&isnum!=4)prec=6;if(isnum==3){O=Vnum.toExponential(1);E=+O.substr(O.indexOf("e")+1);if(prec===0)prec=1;if(prec>E&&E>=-4){isnum=11;prec=prec-(E+1)}else{isnum=12;prec=prec-1}}var sg=Vnum<0||1/Vnum==-Infinity?"-":"";if(Vnum<0)Vnum=-Vnum;switch(isnum){case 1:;case 11:if(Vnum<1e21){O=Vnum.toFixed(prec);if(isnum==1){if(prec===0&&alt&&O.indexOf(".")==-1)O+="."}else if(!alt)O=O.replace(/(\.\d*[1-9])0*$/,"$1").replace(/\.0*$/,"");else if(O.indexOf(".")==-1)O+=".";break}O=Vnum.toExponential(20);E=+O.substr(O.indexOf("e")+1);O=O.charAt(0)+O.substr(2,O.indexOf("e")-2);O=O+(E-O.length+1>=0?"0".repeat(E-O.length+1):"");if(alt||prec>0&&isnum!==11)O=O+"."+(prec>=0?"0".repeat(prec):"");break;case 2:;case 12:O=Vnum.toExponential(prec);E=O.indexOf("e");if(O.length-E===3)O=O.substr(0,E+2)+"0"+O.substr(E+2);if(alt&&O.indexOf(".")==-1)O=O.substr(0,E)+"."+O.substr(E);else if(!alt&&isnum==12)O=O.replace(/\.0*e/,"e").replace(/\.(\d*[1-9])0*e/,".$1e");break;case 4:if(Vnum===0){O="0x0"+(alt||prec>0?"."+(prec>=0?"0".repeat(prec):""):"")+"p+0";break}O=Vnum.toString(16);var ac=O.charCodeAt(0);if(ac==48){ac=2;E=-4;Vnum*=16;while(O.charCodeAt(ac++)==48){E-=4;Vnum*=16}O=Vnum.toString(16);ac=O.charCodeAt(0)}var ai=O.indexOf(".");if(O.indexOf("(")>-1){var am=O.match(/\(e(.*)\)/);var ae=am?+am[1]:0;E+=4*ae;Vnum/=Math.pow(16,ae)}else if(ai>1){E+=4*(ai-1);Vnum/=Math.pow(16,ai-1)}else if(ai==-1){E+=4*(O.length-1);Vnum/=Math.pow(16,O.length-1)}if(bytes>8){if(ac<50){E-=3;Vnum*=8}else if(ac<52){E-=2;Vnum*=4}else if(ac<56){E-=1;Vnum*=2}}else{if(ac>=56){E+=3;Vnum/=8}else if(ac>=52){E+=2;Vnum/=4}else if(ac>=50){E+=1;Vnum/=2}}O=Vnum.toString(16);if(O.length>1){if(O.length>prec+2&&O.charCodeAt(prec+2)>=56){var _f=O.charCodeAt(0)==102;O=(Vnum+8*Math.pow(16,-prec-1)).toString(16);if(_f&&O.charCodeAt(0)==49)E+=4}if(prec>0){O=O.substr(0,prec+2);if(O.length=0?"0".repeat(prec+2-O.length):"")+O.substr(1);else O+=prec+2-O.length>=0?"0".repeat(prec+2-O.length):""}}else if(prec===0)O=O.charAt(0)+(alt?".":"")}else if(prec>0)O=O+"."+(prec>=0?"0".repeat(prec):"");else if(alt)O=O+".";O="0x"+O+"p"+(E>=0?"+"+E:E);break;}if(sg===""){if(flags.indexOf("+")>-1)sg="+";else if(flags.indexOf(" ")>-1)sg=" "}O=sg+O}if(width>O.length){if(flags.indexOf("-")>-1){O=O+(width-O.length>=0?" ".repeat(width-O.length):"")}else if(flags.indexOf("0")>-1&&O.length>0&&isf){pad=width-O.length>=0?"0".repeat(width-O.length):"";if(O.charCodeAt(0)<48){if(O.charAt(2).toLowerCase()=="x")O=O.substr(0,3)+pad+O.substring(3);else O=O.substr(0,1)+pad+O.substring(1)}else if(O.charAt(1).toLowerCase()=="x")O=O.substr(0,2)+pad+O.substring(2);else O=pad+O}else{O=(width-O.length>=0?" ".repeat(width-O.length):"")+O}}if(c<96)O=O.toUpperCase()}o+=O}return o}function vsprintf(fmt,args){return doit(tokenize(fmt),args)}function sprintf(){var args=new Array(arguments.length-1);for(var i=0;i Date: Wed, 27 Apr 2022 01:29:21 -0400 Subject: [PATCH 15/15] replace git.io links --- index.html | 4 ++-- tests/mjs.html | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 835716f..c11469f 100644 --- a/index.html +++ b/index.html @@ -14,8 +14,8 @@ table, td { PRINTJ Live Demo
-Source Code Repo
-Issues? Something look weird? Click here and report an issue
+Source Code Repo
+Issues? Something look weird? Click here and report an issue

 usage: var printf = PRINTJ.sprintf;
diff --git a/tests/mjs.html b/tests/mjs.html
index 922a60f..3a5d4f8 100644
--- a/tests/mjs.html
+++ b/tests/mjs.html
@@ -14,8 +14,8 @@ table, td {
 
 
 PRINTJ ESM Live Demo
-Source Code Repo
-Issues? Something look weird? Click here and report an issue
+Source Code Repo
+Issues? Something look weird? Click here and report an issue

 usage: import { sprintf as printf } from './printj.mjs';
-- 
2.34.1