version bump 1.0.1: typings

- typescript definitions and types
- flowtype definition refinement
This commit is contained in:
SheetJS 2017-07-04 02:26:51 -04:00
parent 4838519c2c
commit ed55a202b3
25 changed files with 172 additions and 69 deletions

View File

@ -2,7 +2,6 @@
.*/node_modules/.*
.*/dist/.*
.*/test.js
.*/printj.js
.*/bits/.*
.*/ctest/.*
@ -13,6 +12,8 @@
.*/demo/browser.js
.*/shim.js
.*/printj.js
[include]
printj.flow.js
.*/bin/.*.njs

View File

@ -84,6 +84,10 @@ old-lint: $(TARGET) ## Run jshint and jscs checks
@jscs lib/*.js
if [ -e $(CLOSURE) ]; then java -jar $(CLOSURE) $(FLOWTARGET) --jscomp_warning=reportUnknownTypes >/dev/null; fi
.PHONY: tslint
tslint: $(TARGET) ## Run typescript checks
#@npm install dtslint typescript
@npm run-script dtslint
.PHONY: flow
flow: lint ## Run flow checker

View File

@ -33,11 +33,11 @@ function help() {
}
function parse_arg(arg/*:string*/)/*:any*/ {
var m = arg.substr(2), p/*:number*/;
if(arg.charCodeAt(1) == 58) switch((p = arg.charCodeAt(0))) {
case /*n*/ 110: return parseInt(m);
var m = arg.substr(2), 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 /*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;
@ -56,7 +56,7 @@ for(var i = 2; i < argv.length; ++i) switch(argv[i]) {
console.log(X.vsprintf(fmt, args));
process.exit(0);
function dump(fmt) {
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;

View File

@ -2,9 +2,9 @@
/* vim: set ts=2: */
/*jshint sub:true, eqnull:true */
/*exported PRINTJ */
/*:: declare var DO_NOT_EXPORT_PRINTJ: any; */
/*:: declare var define: any; */
var PRINTJ/*:any*/;
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
/*:: declare function define(cb:()=>any):void; */
var PRINTJ/*:PRINTJModule*/;
(function (factory/*:(a:any)=>void*/)/*:void*/ {
/*jshint ignore:start */
if(typeof DO_NOT_EXPORT_PRINTJ === 'undefined') {
@ -12,16 +12,16 @@ var PRINTJ/*:any*/;
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
var module/*:any*/ = {};
var module/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/;
factory(module);
return module;
});
} else {
factory(PRINTJ = {});
factory(PRINTJ = /*::(*/{}/*:: :any)*/);
}
} else {
factory(PRINTJ = {});
factory(PRINTJ = /*::(*/{}/*:: :any)*/);
}
/*jshint ignore:end */
}(function(PRINTJ) {
}(function(PRINTJ/*:PRINTJModule*/) {
#include "01_version.js"

View File

@ -1 +1 @@
PRINTJ.version = '1.0.0';
PRINTJ.version = '1.0.1';

View File

@ -1,6 +1,6 @@
#define isnan isNaN
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
var padstr = {
var padstr/*:{[s:string]:string}*/ = {
" ": " ",
"0": "000000000000000000000000000000000",
"7": "777777777777777777777777777777777",
@ -37,7 +37,7 @@ function pads(x/*:number*/, c/*:string*/)/*:string*/ { return PAD_(x,c); }
#error SIZEOF_WCHAR_T must be 1, 2, or 4
#endif
#define CHAR_TO_STR(O,cc) cc &= MASK_CHAR; O = String.fromCharCode(cc);
#define CHAR_TO_STR(O,cc) { cc &= MASK_CHAR; O = String.fromCharCode(cc); }
#if SIZEOF_SIZE_T > 4 /* TODO: negative ptrs? */
#define CONV_SIZE_T(x) x = Math.abs(x);

View File

@ -1,7 +1,6 @@
#include "30_ctypes.js"
#include "40_macros.js"
/*:: declare var util:any; */
/*:: declare var require: any; */
/*:: var util = require('util'); */
/*global process:true, util:true, require:true */
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
@ -12,8 +11,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
var Vnum/*:number*/ = 0;
var pad/*:string*/ = "";
for(var i = 0; i < t.length; ++i) {
var m/*:Array<any>*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
for(var i/*:number*/ = 0; i < t.length; ++i) {
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; }
@ -24,7 +23,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
/* flags */
var flags/*:string*/ = m[IDX_FLAGS]||"";
var alt = flags.indexOf("#") > -1;
var alt/*:boolean*/ = flags.indexOf("#") > -1;
/* position */
if(m[IDX_POS]) argidx = parseInt(m[IDX_POS])-1;

View File

@ -15,7 +15,7 @@
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) WCHAR_TO_STR(O, cc)
else CHAR_TO_STR(O, cc)
break;
case "string": O = arg.charAt(0); break;
case "string": O = /*::(*/arg/*:: :string)*/.charAt(0); break;
default: O = String(arg).charAt(0);
}
WIDTH(O, width, flags)

View File

@ -11,7 +11,7 @@
/* 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=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
continue;
/* process error */

View File

@ -31,7 +31,7 @@
} else {
Vnum = (-Vnum) % 1e16;
var d1/*:Array<number>*/ = [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;
var di/*:number*/ = d1.length - 1;
while(Vnum > 0) {
if((d1[di] -= (Vnum % 10)) < 0) { d1[di] += 10; d1[di-1]--; }
--di; Vnum = Math.floor(Vnum / 10);

View File

@ -1,7 +1,7 @@
Vnum = Number(arg);
if(arg === null) Vnum = 0/0;
if(len == "L") bytes = 12;
var isf = isFinite(Vnum);
var isf/*:boolean*/ = isFinite(Vnum);
if(!isf) { /* Infinity or NaN */
if(Vnum < 0) O = "-";
else if(flags.indexOf("+") > -1) O = "+";
@ -94,7 +94,7 @@
O = Vnum.toString(16);
if(O.length > 1) {
if(O.length > prec+2 && O.charCodeAt(prec+2) >= 56) {
var _f = O.charCodeAt(0) == 102;
var _f/*:boolean*/ = O.charCodeAt(0) == 102;
O = (Vnum + 8 * Math.pow(16, -prec-1)).toString(16);
if(_f && O.charCodeAt(0) == 49) E += 4;
}

View File

@ -1,7 +1,7 @@
function vsprintf(fmt/*:string*/, args/*:Array<any>*/) { return doit(tokenize(fmt), args); }
function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); }
function sprintf()/*:string*/ {
function sprintf(/*:: ...argz*/)/*:string*/ {
var args/*:Array<any>*/ = new Array(arguments.length - 1);
for(var i = 0; i < args.length; ++i) args[i] = arguments[i+1];
for(var i/*:number*/ = 0; i < args.length; ++i) args[i] = arguments[i+1];
return doit(tokenize(arguments[0]), args);
}

View File

@ -1,12 +1,14 @@
/*::
type ParsedFmt = Array<Array<any>>;
type ParsedEntry = Array<string>;
type ParsedFmt = Array<ParsedEntry>;
type Args = Array<any>;
declare module "exit-on-epipe" { };
declare class PRINTJModule {
sprintf(fmt:string, ...args:any):string;
vsprintf(fmt:string, args:Args):string;
_doit(t:ParsedFmt, args:Args):string;
_tokenize(fmt:string):ParsedFmt;
version:string;
sprintf:(fmt:string, ...args:any)=>string;
vsprintf:(fmt:string, args:Args)=>string;
_doit:(t:ParsedFmt, args:Args)=>string;
_tokenize:(fmt:string)=>ParsedFmt;
};
declare module "./" { declare var exports:PRINTJModule };
declare module "../" { declare var exports:PRINTJModule };

View File

@ -23,7 +23,7 @@ var PRINTJ;
/*jshint ignore:end */
}(function(PRINTJ) {
PRINTJ.version = '1.0.0';
PRINTJ.version = '1.0.1';
function tokenize(fmt) {
var out = [];
@ -216,7 +216,7 @@ function doit(t, args) {
case "number":
var cc = arg;
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
else cc &= 0xFF; O = String.fromCharCode( cc);
else { cc &= 0xFF; O = String.fromCharCode( cc); }
break;
case "string": O = arg.charAt(0); break;
default: O = String(arg).charAt(0);

4
dist/printj.js vendored
View File

@ -23,7 +23,7 @@ var PRINTJ;
/*jshint ignore:end */
}(function(PRINTJ) {
PRINTJ.version = '1.0.0';
PRINTJ.version = '1.0.1';
function tokenize(fmt) {
var out = [];
@ -216,7 +216,7 @@ function doit(t, args) {
case "number":
var cc = arg;
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
else cc &= 0xFF; O = String.fromCharCode( cc);
else { cc &= 0xFF; O = String.fromCharCode( cc); }
break;
case "string": O = arg.charAt(0); break;
default: O = String(arg).charAt(0);

2
dist/printj.min.js vendored

File diff suppressed because one or more lines are too long

2
dist/printj.min.map vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "printj",
"version": "1.0.0",
"version": "1.0.1",
"author": "sheetjs",
"description": "Pure-JS printf",
"keywords": [ "printf", "sprintf", "format", "string" ],
@ -8,22 +8,27 @@
"printj": "./bin/printj.njs"
},
"main": "./printj",
"types": "types",
"dependencies": {
},
"devDependencies": {
"mocha":"",
"uglify-js":""
"@sheetjs/uglify-js":"",
"@types/node":"",
"dtslint": "^0.1.2",
"typescript": "2.2.0"
},
"repository": { "type":"git", "url":"git://github.com/SheetJS/printj.git" },
"scripts": {
"test": "make test"
"test": "make test",
"dtslint": "dtslint types"
},
"config": {
"blanket": {
"pattern": "printj.js"
}
},
"homepage": "https://oss.sheetjs.com/printj/",
"homepage": "http://sheetjs.com/opensource",
"files": ["printj.js", "bin/printj.njs", "LICENSE", "README.md", "dist/*.js", "dist/*.map", "dist/LICENSE"],
"bugs": { "url": "https://github.com/SheetJS/printj/issues" },
"license": "Apache-2.0",

View File

@ -2,9 +2,9 @@
/* vim: set ts=2: */
/*jshint sub:true, eqnull:true */
/*exported PRINTJ */
/*:: declare var DO_NOT_EXPORT_PRINTJ: any; */
/*:: declare var define: any; */
var PRINTJ/*:any*/;
/*:: declare var DO_NOT_EXPORT_PRINTJ:?boolean; */
/*:: declare function define(cb:()=>any):void; */
var PRINTJ/*:PRINTJModule*/;
(function (factory/*:(a:any)=>void*/)/*:void*/ {
/*jshint ignore:start */
if(typeof DO_NOT_EXPORT_PRINTJ === 'undefined') {
@ -12,20 +12,20 @@ var PRINTJ/*:any*/;
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
var module/*:any*/ = {};
var module/*:PRINTJModule*/ = /*::(*/{}/*:: :any)*/;
factory(module);
return module;
});
} else {
factory(PRINTJ = {});
factory(PRINTJ = /*::(*/{}/*:: :any)*/);
}
} else {
factory(PRINTJ = {});
factory(PRINTJ = /*::(*/{}/*:: :any)*/);
}
/*jshint ignore:end */
}(function(PRINTJ) {
}(function(PRINTJ/*:PRINTJModule*/) {
PRINTJ.version = '1.0.0';
PRINTJ.version = '1.0.1';
function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
var out/*:ParsedFmt*/ = [];
@ -150,15 +150,14 @@ function tokenize(fmt/*:string*/)/*:ParsedFmt*/ {
}
//#define PAD_(x,c) (x >= 0 ? new Array(((x)|0) + 1).join((c)) : "")
var padstr = {
var padstr/*:{[s:string]:string}*/ = {
" ": " ",
"0": "000000000000000000000000000000000",
"7": "777777777777777777777777777777777",
"f": "fffffffffffffffffffffffffffffffff"
};
/*:: declare var util:any; */
/*:: declare var require: any; */
/*:: var util = require('util'); */
/*global process:true, util:true, require:true */
if(typeof process !== 'undefined' && !!process.versions && !!process.versions.node) util=require("util");
var u_inspect/*:(o:any)=>string*/ = (typeof util != 'undefined') ? util.inspect : JSON.stringify;
@ -168,8 +167,8 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
var argidx/*:number*/ = 0, idx/*:number*/ = 0;
var Vnum/*:number*/ = 0;
var pad/*:string*/ = "";
for(var i = 0; i < t.length; ++i) {
var m/*:Array<any>*/ = t[i], c/*:number*/ = (m[0]/*:string*/).charCodeAt(0);
for(var i/*:number*/ = 0; i < t.length; ++i) {
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; }
@ -180,7 +179,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
/* flags */
var flags/*:string*/ = m[3]||"";
var alt = flags.indexOf("#") > -1;
var alt/*:boolean*/ = flags.indexOf("#") > -1;
/* position */
if(m[2]) argidx = parseInt(m[2])-1;
@ -220,9 +219,9 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
case "number":
var cc/*:number*/ = arg;
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
else cc &= 0xFF; O = String.fromCharCode( cc);
else { cc &= 0xFF; O = String.fromCharCode( cc); }
break;
case "string": O = arg.charAt(0); break;
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; } }
@ -286,7 +285,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
/* 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=0; for(var oo/*:number*/ = 0; oo < o.length; ++oo) arg.len += o[oo].length; }
continue;
/* process error */
@ -386,7 +385,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
} else {
Vnum = (-Vnum) % 1e16;
var d1/*:Array<number>*/ = [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;
var di/*:number*/ = d1.length - 1;
while(Vnum > 0) {
if((d1[di] -= (Vnum % 10)) < 0) { d1[di] += 10; d1[di-1]--; }
--di; Vnum = Math.floor(Vnum / 10);
@ -446,7 +445,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
Vnum = Number(arg);
if(arg === null) Vnum = 0/0;
if(len == "L") bytes = 12;
var isf = isFinite(Vnum);
var isf/*:boolean*/ = isFinite(Vnum);
if(!isf) { /* Infinity or NaN */
if(Vnum < 0) O = "-";
else if(flags.indexOf("+") > -1) O = "+";
@ -538,7 +537,7 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
O = Vnum.toString(16);
if(O.length > 1) {
if(O.length > prec+2 && O.charCodeAt(prec+2) >= 56) {
var _f = O.charCodeAt(0) == 102;
var _f/*:boolean*/ = O.charCodeAt(0) == 102;
O = (Vnum + 8 * Math.pow(16, -prec-1)).toString(16);
if(_f && O.charCodeAt(0) == 49) E += 4;
}
@ -588,11 +587,11 @@ function doit(t/*:ParsedFmt*/, args/*:Array<any>*/)/*:string*/ {
return o.join("");
}
function vsprintf(fmt/*:string*/, args/*:Array<any>*/) { return doit(tokenize(fmt), args); }
function vsprintf(fmt/*:string*/, args/*:Args*/)/*:string*/ { return doit(tokenize(fmt), args); }
function sprintf()/*:string*/ {
function sprintf(/*:: ...argz*/)/*:string*/ {
var args/*:Array<any>*/ = new Array(arguments.length - 1);
for(var i = 0; i < args.length; ++i) args[i] = arguments[i+1];
for(var i/*:number*/ = 0; i < args.length; ++i) args[i] = arguments[i+1];
return doit(tokenize(arguments[0]), args);
}

View File

@ -23,7 +23,7 @@ var PRINTJ;
/*jshint ignore:end */
}(function(PRINTJ) {
PRINTJ.version = '1.0.0';
PRINTJ.version = '1.0.1';
function tokenize(fmt) {
var out = [];
@ -216,7 +216,7 @@ function doit(t, args) {
case "number":
var cc = arg;
if(c == 67 || len.charCodeAt(0) === /*l*/ 108) { cc &= 0xFFFFFFFF; O = String.fromCharCode( cc); }
else cc &= 0xFF; O = String.fromCharCode( cc);
else { cc &= 0xFF; O = String.fromCharCode( cc); }
break;
case "string": O = arg.charAt(0); break;
default: O = String(arg).charAt(0);

53
types/bin_printj.ts Normal file
View File

@ -0,0 +1,53 @@
/* 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"];
function help() {
[
"usage: printj [options] <format> [args...]",
"",
"Options:",
" -h, --help output usage information",
"",
"Arguments are treated as strings unless prefaced by a type indicator:",
" n:<integer> call parseInt (ex. n:3 -> 3)",
" f:<float> call parseFloat (ex. f:3.1 -> 3.1)",
' b:<boolean> false when lowercase value is "FALSE" or "0", else true',
" s:<string> interpret as string (ex. s:n:3 -> \"n:3\")",
" j:<JSON> interpret as an object using JSON.parse",
" e:<JS> evaluate argument (ex. e:1+1 -> 2, e:\"1\"+1 -> \"11\")",
"",
"samples:",
" $ printj '|%02hhx%d|' n:50 e:0x7B # |32123|",
" $ printj '|%2$d + %3$d is %1$d|' e:1+2 n:1 n:2 # |1 + 2 is 3| ",
" $ 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/"
].forEach(function(l) { console.log(l); });
return 0;
}
function parse_arg(arg: string): any {
let m: string = arg.substr(2), 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 /*s*/ 115: return m;
}
return arg;
}
let args: any[] = [];
let fmt = "", n = 0;
for(let i = 2; i < argv.length; ++i) switch(argv[i]) {
case "--help": case "-h": break;
default: if(n++ === 0) fmt = argv[i]; else args.push(parse_arg(argv[i]));
}
console.log(X.vsprintf(fmt, args));

11
types/index.d.ts vendored Normal file
View File

@ -0,0 +1,11 @@
/* index.d.ts (C) 2015-present SheetJS */
// TypeScript Version: 2.2
/** Version string */
export const version: string;
/** Generate formatted string from format and subsequent arguments */
export function sprintf(fmt: string, ...args: any[]): string;
/** Generate formatted string from format and array of variables */
export function vsprintf(fmt: string, args: any[]): string;

4
types/printj-test.ts Normal file
View File

@ -0,0 +1,4 @@
import { vsprintf, sprintf } from 'printj';
const t1: string = sprintf("%02hhx", 123);
const t2: string = vsprintf("%02hhx %d", [123, 213]);

14
types/tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [ "es5", "dom" ],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"baseUrl": ".",
"paths": { "printj": ["."] },
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
}
}

11
types/tslint.json Normal file
View File

@ -0,0 +1,11 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"no-sparse-arrays": false,
"only-arrow-functions": false,
"no-consecutive-blank-lines": false,
"prefer-conditional-expression": false,
"one-variable-per-declaration": false
}
}