Comparing version 0.8.0 to 0.9.0
import { ChainID, ChainIDParams } from "./chain"; | ||
import { StandardSpec } from "./types"; | ||
import { IdentifierSpec } from "./types"; | ||
export interface AccountIDParams { | ||
@@ -8,4 +8,4 @@ chainId: string | ChainIDParams; | ||
export declare class AccountID { | ||
static spec: StandardSpec; | ||
static parse(accountId: string): AccountID; | ||
static spec: IdentifierSpec; | ||
static parse(id: string): AccountID; | ||
static format(params: AccountIDParams): string; | ||
@@ -12,0 +12,0 @@ chainId: ChainID; |
@@ -9,20 +9,12 @@ "use strict"; | ||
if (typeof params === "string") { | ||
const accountId = AccountID.parse(params); | ||
this.chainId = accountId.chainId; | ||
this.address = accountId.address; | ||
params = AccountID.parse(params); | ||
} | ||
else { | ||
this.chainId = new chain_1.ChainID(params.chainId); | ||
this.address = params.address; | ||
} | ||
this.chainId = new chain_1.ChainID(params.chainId); | ||
this.address = params.address; | ||
} | ||
static parse(accountId) { | ||
if (!utils_1.isValidId(accountId, this.spec)) { | ||
throw new Error(`Invalid accountId provided: ${accountId}`); | ||
static parse(id) { | ||
if (!utils_1.isValidId(id, this.spec)) { | ||
throw new Error(`Invalid ${this.spec.name} provided: ${id}`); | ||
} | ||
const params = utils_1.splitParams(accountId, this.spec); | ||
return new AccountID({ | ||
chainId: params[1], | ||
address: params[0], | ||
}); | ||
return new AccountID(utils_1.getParams(id, this.spec)); | ||
} | ||
@@ -29,0 +21,0 @@ static format(params) { |
@@ -1,2 +0,2 @@ | ||
import { StandardSpec } from "./types"; | ||
import { IdentifierSpec } from "./types"; | ||
export interface ChainIDParams { | ||
@@ -7,4 +7,4 @@ namespace: string; | ||
export declare class ChainID { | ||
static spec: StandardSpec; | ||
static parse(chainId: string): ChainID; | ||
static spec: IdentifierSpec; | ||
static parse(id: string): ChainID; | ||
static format(params: ChainIDParams): string; | ||
@@ -11,0 +11,0 @@ namespace: string; |
@@ -8,23 +8,15 @@ "use strict"; | ||
if (typeof params === "string") { | ||
const chainId = ChainID.parse(params); | ||
this.namespace = chainId.namespace; | ||
this.reference = chainId.reference; | ||
params = ChainID.parse(params); | ||
} | ||
else { | ||
this.namespace = params.namespace; | ||
this.reference = params.reference; | ||
} | ||
this.namespace = params.namespace; | ||
this.reference = params.reference; | ||
} | ||
static parse(chainId) { | ||
if (!utils_1.isValidId(chainId, this.spec)) { | ||
throw new Error(`Invalid chainId provided: ${chainId}`); | ||
static parse(id) { | ||
if (!utils_1.isValidId(id, this.spec)) { | ||
throw new Error(`Invalid ${this.spec.name} provided: ${id}`); | ||
} | ||
const params = utils_1.splitParams(chainId, this.spec); | ||
return new ChainID({ | ||
namespace: params[0], | ||
reference: params[1], | ||
}); | ||
return new ChainID(utils_1.getParams(id, this.spec)); | ||
} | ||
static format(params) { | ||
return params.namespace + this.spec.delimiter + params.reference; | ||
return utils_1.joinParams(params, this.spec); | ||
} | ||
@@ -31,0 +23,0 @@ toString() { |
@@ -1,6 +0,6 @@ | ||
import { StandardSpec } from "./types"; | ||
import { IdentifierSpec } from "./types"; | ||
export declare const CAIP: { | ||
"2": StandardSpec; | ||
"10": StandardSpec; | ||
"2": IdentifierSpec; | ||
"10": IdentifierSpec; | ||
}; | ||
//# sourceMappingURL=spec.d.ts.map |
@@ -5,13 +5,15 @@ "use strict"; | ||
name: "chainId", | ||
delimiter: ":", | ||
regex: "[-:a-zA-Z0-9]{5,64}", | ||
parameters: { | ||
0: { | ||
name: "namespace", | ||
regex: "[-a-z0-9]{3,16}", | ||
delimiter: ":", | ||
values: { | ||
0: { | ||
name: "namespace", | ||
regex: "[-a-z0-9]{3,16}", | ||
}, | ||
1: { | ||
name: "reference", | ||
regex: "[-a-zA-Z0-9]{1,47}", | ||
}, | ||
}, | ||
1: { | ||
name: "reference", | ||
regex: "[-a-zA-Z0-9]{1,47}", | ||
}, | ||
}, | ||
@@ -21,10 +23,12 @@ }; | ||
name: "accountId", | ||
delimiter: "@", | ||
regex: "[-@:a-zA-Z0-9]{7,128}", | ||
parameters: { | ||
0: { | ||
name: "address", | ||
regex: "[a-zA-Z0-9]{1,63}", | ||
delimiter: "@", | ||
values: { | ||
0: { | ||
name: "address", | ||
regex: "[a-zA-Z0-9]{1,63}", | ||
}, | ||
1: CAIP2, | ||
}, | ||
1: CAIP2, | ||
}, | ||
@@ -31,0 +35,0 @@ }; |
@@ -5,6 +5,8 @@ export interface ParameterSpec { | ||
} | ||
export interface StandardSpec extends ParameterSpec { | ||
delimiter: string; | ||
export interface IdentifierSpec extends ParameterSpec { | ||
parameters: { | ||
[index: string]: ParameterSpec; | ||
delimiter: string; | ||
values: { | ||
[index: string]: ParameterSpec; | ||
}; | ||
}; | ||
@@ -11,0 +13,0 @@ } |
@@ -1,5 +0,6 @@ | ||
import { StandardSpec, Params } from "./types"; | ||
export declare function splitParams(id: string, spec: StandardSpec): string[]; | ||
export declare function joinParams(params: Params, spec: StandardSpec): string; | ||
export declare function isValidId(id: string, spec: StandardSpec): boolean; | ||
import { IdentifierSpec, Params } from "./types"; | ||
export declare function splitParams(id: string, spec: IdentifierSpec): string[]; | ||
export declare function getParams<T>(id: string, spec: IdentifierSpec): T; | ||
export declare function joinParams(params: Params, spec: IdentifierSpec): string; | ||
export declare function isValidId(id: string, spec: IdentifierSpec): boolean; | ||
//# sourceMappingURL=utils.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function splitParams(id, spec) { | ||
return id.split(spec.delimiter); | ||
return id.split(spec.parameters.delimiter); | ||
} | ||
exports.splitParams = splitParams; | ||
function getParams(id, spec) { | ||
const arr = splitParams(id, spec); | ||
const params = {}; | ||
arr.forEach((value, index) => { | ||
params[spec.parameters.values[index].name] = value; | ||
}); | ||
return params; | ||
} | ||
exports.getParams = getParams; | ||
function joinParams(params, spec) { | ||
return Object.values(spec.parameters) | ||
return Object.values(spec.parameters.values) | ||
.map(parameter => { | ||
@@ -15,3 +24,3 @@ const param = params[parameter.name]; | ||
}) | ||
.join(spec.delimiter); | ||
.join(spec.parameters.delimiter); | ||
} | ||
@@ -23,6 +32,6 @@ exports.joinParams = joinParams; | ||
const params = splitParams(id, spec); | ||
if (params.length !== Object.keys(spec.parameters).length) | ||
if (params.length !== Object.keys(spec.parameters.values).length) | ||
return false; | ||
const matches = params | ||
.map((param, index) => new RegExp(spec.parameters[index].regex).test(param)) | ||
.map((param, index) => new RegExp(spec.parameters.values[index].regex).test(param)) | ||
.filter(x => !!x); | ||
@@ -29,0 +38,0 @@ if (matches.length !== params.length) |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("CAIP",[],t):"object"==typeof exports?exports.CAIP=t():e.CAIP=t()}(this,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var s=t[n]={i:n,l:!1,exports:{}};return e[n].call(s.exports,s,s.exports,r),s.l=!0,s.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)r.d(n,s,function(t){return e[t]}.bind(null,s));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=3)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n=r(1),s=r(2);class i{constructor(e){if("string"==typeof e){const t=i.parse(e);this.namespace=t.namespace,this.reference=t.reference}else this.namespace=e.namespace,this.reference=e.reference}static parse(e){if(!s.isValidId(e,this.spec))throw new Error("Invalid chainId provided: "+e);const t=s.splitParams(e,this.spec);return new i({namespace:t[0],reference:t[1]})}static format(e){return e.namespace+this.spec.delimiter+e.reference}toString(){return i.format(this.toJson())}toJson(){return{namespace:this.namespace,reference:this.reference}}}t.ChainID=i,i.spec=n.CAIP[2]},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n={name:"chainId",delimiter:":",regex:"[-:a-zA-Z0-9]{5,64}",parameters:{0:{name:"namespace",regex:"[-a-z0-9]{3,16}"},1:{name:"reference",regex:"[-a-zA-Z0-9]{1,47}"}}},s={name:"accountId",delimiter:"@",regex:"[-@:a-zA-Z0-9]{7,128}",parameters:{0:{name:"address",regex:"[a-zA-Z0-9]{1,63}"},1:n}};t.CAIP={2:n,10:s}},function(e,t,r){"use strict";function n(e,t){return e.split(t.delimiter)}Object.defineProperty(t,"__esModule",{value:!0}),t.splitParams=n,t.joinParams=function e(t,r){return Object.values(r.parameters).map(r=>{const n=t[r.name];return"string"==typeof n?n:e(n,r)}).join(r.delimiter)},t.isValidId=function(e,t){if(!new RegExp(t.regex).test(e))return!1;const r=n(e,t);return r.length===Object.keys(t.parameters).length&&r.map((e,r)=>new RegExp(t.parameters[r].regex).test(e)).filter(e=>!!e).length===r.length}},function(e,t,r){"use strict";function n(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),n(r(4)),n(r(0))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n=r(0),s=r(1),i=r(2);class a{constructor(e){if("string"==typeof e){const t=a.parse(e);this.chainId=t.chainId,this.address=t.address}else this.chainId=new n.ChainID(e.chainId),this.address=e.address}static parse(e){if(!i.isValidId(e,this.spec))throw new Error("Invalid accountId provided: "+e);const t=i.splitParams(e,this.spec);return new a({chainId:t[1],address:t[0]})}static format(e){return i.joinParams(e,this.spec)}toString(){return a.format(this.toJson())}toJson(){return{chainId:this.chainId.toJson(),address:this.address}}}t.AccountID=a,a.spec=s.CAIP[10]}])})); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("CAIP",[],t):"object"==typeof exports?exports.CAIP=t():e.CAIP=t()}(this,(function(){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var s=t[n]={i:n,l:!1,exports:{}};return e[n].call(s.exports,s,s.exports,r),s.l=!0,s.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)r.d(n,s,function(t){return e[t]}.bind(null,s));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=3)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n=r(1),s=r(2);class a{constructor(e){"string"==typeof e&&(e=a.parse(e)),this.namespace=e.namespace,this.reference=e.reference}static parse(e){if(!s.isValidId(e,this.spec))throw new Error(`Invalid ${this.spec.name} provided: ${e}`);return new a(s.getParams(e,this.spec))}static format(e){return s.joinParams(e,this.spec)}toString(){return a.format(this.toJson())}toJson(){return{namespace:this.namespace,reference:this.reference}}}t.ChainID=a,a.spec=n.CAIP[2]},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n={name:"chainId",regex:"[-:a-zA-Z0-9]{5,64}",parameters:{delimiter:":",values:{0:{name:"namespace",regex:"[-a-z0-9]{3,16}"},1:{name:"reference",regex:"[-a-zA-Z0-9]{1,47}"}}}},s={name:"accountId",regex:"[-@:a-zA-Z0-9]{7,128}",parameters:{delimiter:"@",values:{0:{name:"address",regex:"[a-zA-Z0-9]{1,63}"},1:n}}};t.CAIP={2:n,10:s}},function(e,t,r){"use strict";function n(e,t){return e.split(t.parameters.delimiter)}Object.defineProperty(t,"__esModule",{value:!0}),t.splitParams=n,t.getParams=function(e,t){const r=n(e,t),s={};return r.forEach((e,r)=>{s[t.parameters.values[r].name]=e}),s},t.joinParams=function e(t,r){return Object.values(r.parameters.values).map(r=>{const n=t[r.name];return"string"==typeof n?n:e(n,r)}).join(r.parameters.delimiter)},t.isValidId=function(e,t){if(!new RegExp(t.regex).test(e))return!1;const r=n(e,t);return r.length===Object.keys(t.parameters.values).length&&r.map((e,r)=>new RegExp(t.parameters.values[r].regex).test(e)).filter(e=>!!e).length===r.length}},function(e,t,r){"use strict";function n(e){for(var r in e)t.hasOwnProperty(r)||(t[r]=e[r])}Object.defineProperty(t,"__esModule",{value:!0}),n(r(4)),n(r(0))},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});const n=r(0),s=r(1),a=r(2);class o{constructor(e){"string"==typeof e&&(e=o.parse(e)),this.chainId=new n.ChainID(e.chainId),this.address=e.address}static parse(e){if(!a.isValidId(e,this.spec))throw new Error(`Invalid ${this.spec.name} provided: ${e}`);return new o(a.getParams(e,this.spec))}static format(e){return a.joinParams(e,this.spec)}toString(){return o.format(this.toJson())}toJson(){return{chainId:this.chainId.toJson(),address:this.address}}}t.AccountID=o,o.spec=s.CAIP[10]}])})); |
{ | ||
"name": "caip", | ||
"description": "CAIP standard utils", | ||
"version": "0.8.0", | ||
"version": "0.9.0", | ||
"author": "Pedro Gomes <github.com/pedrouid>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128166