From cc97a5806af370adb28d251e212fb02ed20c3061 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sun, 27 Aug 2023 14:04:33 +0200 Subject: [PATCH] Add types, etc. --- crosshash.d.ts | 12 ++++++++++++ crosshash.js | 22 ++++++++-------------- package.json | 6 ++++-- pyproject.toml | 2 +- 4 files changed, 25 insertions(+), 17 deletions(-) create mode 100644 crosshash.d.ts diff --git a/crosshash.d.ts b/crosshash.d.ts new file mode 100644 index 0000000..2034d72 --- /dev/null +++ b/crosshash.d.ts @@ -0,0 +1,12 @@ +type JSONValue = + | string + | number + | boolean + | { [x: string]: JSONValue } + | Array + +export declare function crosshash(obj: JSONValue): string; + +export declare function crossjson(obj: JSONValue): string; + +export declare class CrossHashError extends Error {} diff --git a/crosshash.js b/crosshash.js index 1350787..8aec527 100755 --- a/crosshash.js +++ b/crosshash.js @@ -15,7 +15,6 @@ class CrossHashError extends Error { const crosshash = (obj) => { return md5(crossjson(obj)) } - const crossjson = (obj) => { return stringify(obj, {replacer: replacer}) } @@ -46,24 +45,19 @@ const main = () => { Usage: node crosshash.js --json '{"foo": "bar"}' node crosshash.js --hash '{"foo": "bar"}' - `; - - const args = process.argv.slice(2); // remove the first two arguments (node and script name) - + ` + const args = process.argv.slice(2) if (args.length !== 2 || !['--json', '--hash'].includes(args[0])) { - console.log(usage); - process.exit(1); + console.log(usage) + process.exit(1) } - - const action = args[0]; - const inputJson = JSON.parse(args[1]); + const action = args[0] + const inputJson = JSON.parse(args[1]) const operation = { '--json': crossjson, '--hash': crosshash - }[action]; - - const output = operation(inputJson); - return output; + }[action] + return operation(inputJson) } if (typeof require !== 'undefined' && typeof module !== 'undefined' && require.main === module) { diff --git a/package.json b/package.json index 0f9b5e5..958c409 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,14 @@ { "name": "crosshash", - "version": "0.2.0", + "version": "0.3.1", "description": "Stable, cross-platform JSON serialization and hashing for Python and JavaScript.", "homepage": "https://github.com/httpie/crosshash", "license": "MIT", "main": "crosshash.js", + "types": "crosshash.d.ts", "files": [ - "crosshash.js" + "crosshash.js", + "crosshash.d.ts" ], "bin": { "crosshash": "./crosshash.js", diff --git a/pyproject.toml b/pyproject.toml index c820e29..29b68d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "crosshash" -version = "0.2.0" +version = "0.3.1" authors = ["Jakub Roztocil "] description = "Stable, cross-platform JSON serialization and hashing for Python and JavaScript." license = "MIT"