Skip to content

Commit

Permalink
Add types, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Aug 27, 2023
1 parent 0f3a499 commit cc97a58
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
12 changes: 12 additions & 0 deletions crosshash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| Array<JSONValue>

export declare function crosshash(obj: JSONValue): string;

export declare function crossjson(obj: JSONValue): string;

export declare class CrossHashError extends Error {}
22 changes: 8 additions & 14 deletions crosshash.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class CrossHashError extends Error {
const crosshash = (obj) => {
return md5(crossjson(obj))
}

const crossjson = (obj) => {
return stringify(obj, {replacer: replacer})
}
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "crosshash"
version = "0.2.0"
version = "0.3.1"
authors = ["Jakub Roztocil <[email protected]>"]
description = "Stable, cross-platform JSON serialization and hashing for Python and JavaScript."
license = "MIT"
Expand Down

0 comments on commit cc97a58

Please sign in to comment.