From 783f6531199ae9c0683481a6468bfd78bca566c0 Mon Sep 17 00:00:00 2001 From: Bill Barman Date: Thu, 22 Nov 2018 23:30:07 +0800 Subject: [PATCH] better default parameter parsing --- package-lock.json | 16 ++++++++-------- src/libDDO/DDO.ts | 18 ++++++------------ test/libDDO/DDO.test.ts | 4 ++-- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 314620b..2441c0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -157,7 +157,7 @@ }, "@types/events": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/@types/events/-/events-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz", "integrity": "sha512-KEIlhXnIutzKwRbQkGWb/I4HFqBuUykAdHgDED6xqwXJfONCjF5VoE0cXEiurh3XauygxzeDzgtXUqvLkxFzzA==", "dev": true }, @@ -1435,7 +1435,7 @@ "dependencies": { "pify": { "version": "2.3.0", - "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } @@ -1500,7 +1500,7 @@ "dependencies": { "file-type": { "version": "3.9.0", - "resolved": "http://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz", "integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek=" }, "get-stream": { @@ -1514,7 +1514,7 @@ }, "pify": { "version": "2.3.0", - "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", "integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw=" } } @@ -3764,7 +3764,7 @@ }, "media-typer": { "version": "0.3.0", - "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, "mem": { @@ -5519,7 +5519,7 @@ }, "path-is-absolute": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-key": { @@ -6523,7 +6523,7 @@ }, "tar": { "version": "2.2.1", - "resolved": "http://registry.npmjs.org/tar/-/tar-2.2.1.tgz", + "resolved": "https://registry.npmjs.org/tar/-/tar-2.2.1.tgz", "integrity": "sha1-jk0qJWwOIYXGsYrWlK7JaLg8sdE=", "requires": { "block-stream": "0.0.9", @@ -7101,7 +7101,7 @@ }, "utf8": { "version": "2.1.1", - "resolved": "http://registry.npmjs.org/utf8/-/utf8-2.1.1.tgz", + "resolved": "https://registry.npmjs.org/utf8/-/utf8-2.1.1.tgz", "integrity": "sha1-LgHbAvfY0JRPdxBPFgnrDDBM92g=" }, "util": { diff --git a/src/libDDO/DDO.ts b/src/libDDO/DDO.ts index 7ec7faf..d791991 100644 --- a/src/libDDO/DDO.ts +++ b/src/libDDO/DDO.ts @@ -96,7 +96,6 @@ export default class DDO { if (typeof did === "object") { this.readFromData(did) } - } /* @@ -195,10 +194,8 @@ export default class DDO { * */ public addSignature(encoding?: string, isEmbedded?: boolean): string { - if ( encoding == null ) { - encoding = DDO.ENCODING_TYPES.PEM - } - isEmbedded = isEmbedded == null ? false : isEmbedded + encoding = encoding ? encoding : DDO.ENCODING_TYPES.PEM + isEmbedded = isEmbedded ? isEmbedded : false encoding = encoding.toLowerCase() // generate the key pairs @@ -262,10 +259,8 @@ export default class DDO { * hash text * */ - public addProof(authIndex, privateKey, signatureText?) { - if ( authIndex == null ) { - authIndex = 0 - } + public addProof(privateKey: string, authIndex?: number, signatureText?: string) { + authIndex = authIndex ? authIndex : 0 const authentication = this.authentications[authIndex] let publicKey = null @@ -531,9 +526,8 @@ export default class DDO { * */ public validateProof(signatureText?: string): boolean { - if ( signatureText == null ) { - signatureText = this.hashTextList().join() - } + signatureText = signatureText ? signatureText : this.hashTextList().join() + if ( !this.isProofDefined() ) { return false } diff --git a/test/libDDO/DDO.test.ts b/test/libDDO/DDO.test.ts index 2332e90..d9ce173 100644 --- a/test/libDDO/DDO.test.ts +++ b/test/libDDO/DDO.test.ts @@ -141,7 +141,7 @@ describe("libDDO", () => { assert(ddo) const privateKey = ddo.addSignature() assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----")) - ddo.addProof(0, privateKey) + ddo.addProof(privateKey) // console.log(ddo.toJSON()) assert(ddo.validateProof()) }) @@ -152,7 +152,7 @@ describe("libDDO", () => { assert(ddo) const privateKey = ddo.addSignature("pem", true) assert(privateKey.match("-----BEGIN RSA PRIVATE KEY-----")) - ddo.addProof(0, privateKey) + ddo.addProof(privateKey) // console.log(ddo.toJSON()) assert(ddo.validateProof()) })