UNPKG

@cosmwasm/ts-codegen

Version:

@cosmwasm/ts-codegen converts your CosmWasm smart contracts into dev-friendly TypeScript classes so you can focus on shipping code.

90 lines (89 loc) 2.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanse = void 0; const case_1 = require("case"); const cleanFor = (str) => { /* 1. look at first char after _for_ 2. ONLY if you find capitals after, modify it */ while (/_[a-z]+_[A-Z]/.test(str)) { const m = str.match(/(_[a-z]+_)[A-Z]/); str = str.replace(m[1], (0, case_1.pascal)(m[1])); } return str; }; const cleanNullable = (str) => { if (/^Nullable_/.test(str)) { str = str.replace(/^Nullable_/, 'Nullable'); } return str; }; const cleanse = (obj) => { let copy; // Handle the 3 simple types, and null or undefined if (null == obj || 'object' != typeof obj) return obj; // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (let i = 0, len = obj.length; i < len; i++) { copy[i] = (0, exports.cleanse)(obj[i]); } return copy; } // Handle Object if (obj instanceof Object || typeof obj === 'object') { copy = {}; // https://github.com/CosmWasm/cosmwasm-typescript-gen/issues/27 if (Array.isArray(obj.enum) && obj.enum.length === 0) { delete obj.enum; if (!obj.type) { obj.type = 'string'; } } for (let attr in obj) { if (obj.hasOwnProperty(attr)) { if (/_for_/.test(attr)) { // @ts-ignore copy[cleanFor(attr)] = (0, exports.cleanse)(obj[attr]); } else if (/^Nullable_/.test(attr)) { // @ts-ignore copy[cleanNullable(attr)] = (0, exports.cleanse)(obj[attr]); } else { switch (attr) { case 'title': case '$ref': if (typeof obj[attr] === 'string') { // @ts-ignore copy[attr] = (0, exports.cleanse)(cleanNullable(cleanFor(obj[attr]))); } else { // @ts-ignore copy[attr] = (0, exports.cleanse)(obj[attr]); } break; default: // @ts-ignore copy[attr] = (0, exports.cleanse)(obj[attr]); } } } else { // @ts-ignore copy[attr] = (0, exports.cleanse)(obj[attr]); } } return copy; } throw new Error("Unable to copy obj! Its type isn't supported."); }; exports.cleanse = cleanse;