mirror of
https://github.com/oceanprotocol-archive/squid-js.git
synced 2024-02-02 15:31:51 +01:00
Add conversion helpers for did:op:
prefixes.
This commit is contained in:
parent
136250b70c
commit
e7b9f990b6
@ -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]}
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user