From e7b9f990b6084e22f3905ee9999c18bd19f04d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Guti=C3=A9rrez?= Date: Mon, 11 Mar 2019 22:46:27 +0100 Subject: [PATCH] Add conversion helpers for `did:op:` prefixes. --- src/utils/ConversionTypeHelpers.ts | 38 +++++++++++++++++++----- test/utils/ConversionTypeHelpers.test.ts | 37 +++++++++++++++++++++-- 2 files changed, 64 insertions(+), 11 deletions(-) diff --git a/src/utils/ConversionTypeHelpers.ts b/src/utils/ConversionTypeHelpers.ts index ee71225..50cee05 100644 --- a/src/utils/ConversionTypeHelpers.ts +++ b/src/utils/ConversionTypeHelpers.ts @@ -1,13 +1,35 @@ import { Logger } from './Logger' +// Ox transformer export const zeroX = (input: string) => zeroXTransformer(input, true) export const noZeroX = (input: string) => zeroXTransformer(input, false) - -export function zeroXTransformer(input: string, zeroOutput: boolean) { - const match = input.match(/^(?:0x)?([a-f0-9]+)$/i) - if (!match) { - Logger.warn(`Input transformation failed.`) - return input - } - return (zeroOutput ? "0x" : "") + match[1] +export function zeroXTransformer(input: string = "", zeroOutput: boolean) { + const {valid, output} = inputMatch(input, /^(?:0x)*([a-f0-9]+)$/i, "zeroXTransformer") + return (zeroOutput && valid ? "0x" : "") + output +} + +// did:op: transformer +export const didPrefixed = (input: string) => didTransformer(input, true) +export const noDidPrefixed = (input: string) => didTransformer(input, false) +export function didTransformer(input: string = "", prefixOutput: boolean) { + const {valid, output} = inputMatch(input, /^(?:0x|did:op:)*([a-f0-9]{64})$/i, "didTransformer") + return (prefixOutput && valid ? "did:op:" : "") + output +} + +// 0x + did:op: transformer +export const didZeroX = (input: string) => zeroX(didTransformer(input, false)) + +// Shared functions +function inputMatch(input: string, regexp: RegExp, conversorName: string): {valid: boolean, output: string} { + if (typeof input !== "string") { + Logger.debug("Not input string:") + Logger.debug(input) + throw new Error(`[${conversorName}] Expected string, input type: ${typeof input}`); + } + const match = input.match(regexp) + if (!match) { + Logger.warn(`[${conversorName}] Input transformation failed.`) + return {valid: false, output: input} + } + return {valid: true, output: match[1]} } diff --git a/test/utils/ConversionTypeHelpers.test.ts b/test/utils/ConversionTypeHelpers.test.ts index 143ee76..33ab47e 100644 --- a/test/utils/ConversionTypeHelpers.test.ts +++ b/test/utils/ConversionTypeHelpers.test.ts @@ -1,8 +1,8 @@ import { assert } from "chai" -import { zeroX, noZeroX } from "../../src/utils/ConversionTypeHelpers" +import { zeroX, noZeroX, didPrefixed, noDidPrefixed } from "../../src/utils/ConversionTypeHelpers" -describe("ConversionTypeHelpers", () => { - describe("#zeroX()", () => { +describe.only("ConversionTypeHelpers", () => { + describe("#zeroXTransformer()", () => { it("should return the input if it's not hex value", async () => { const result1 = zeroX("Test 1") const result2 = noZeroX("Test 2") @@ -24,4 +24,35 @@ describe("ConversionTypeHelpers", () => { assert.equal(result2, "1234") }) }) + + describe("#didTransformer()", () => { + const did = "a".repeat(64) + + it("should return the input if it's not valid", async () => { + const result1 = didPrefixed("Test 1") + const result2 = noDidPrefixed("Test 2") + const result3 = noDidPrefixed("Test 3") + assert.equal(result1, "Test 1") + assert.equal(result2, "Test 2") + assert.equal(result3, "Test 3") + }) + + it("should return the value with did:op: prefix", async () => { + const result1 = didPrefixed(`0x${did}`) + const result2 = didPrefixed(did) + const result3 = didPrefixed(`did:op:${did}`) + assert.equal(result1, `did:op:${did}`) + assert.equal(result2, `did:op:${did}`) + assert.equal(result3, `did:op:${did}`) + }) + + it("should return the value without did:op: prefix", async () => { + const result1 = noDidPrefixed(`0x${did}`) + const result2 = noDidPrefixed(did) + const result3 = noDidPrefixed(`did:op:${did}`) + assert.equal(result1, did) + assert.equal(result2, did) + assert.equal(result3, did) + }) + }) })