Skip to content

对象

object | array 复杂的数据类型进行掩码处理,也可自定义规则。

对象

ts
import { maskObject } from 'maskio';

const source = {
  firstCode: '123456',
  otherCode: {
    resultCode: '123',
    name: '小明',
  },
};
const config = { '^.*Code$': 'all', '^otherCode.name$': 'userName' };
console.log(maskObject(source, config));

// 将所有带Code结尾的字段进行全掩码,对otherCode.name字段进行精准匹配,使用name预设掩码。
// 输出:{"firstCode": "******","otherCode": {"resultCode": "***","name": "*明"}}

数组对象

ts
import { maskObject } from 'maskio';

const sourceArr = [
  123,
  {
    firstCode: '123456',
    otherCode: [
      {
        resultCode: '123',
        other: '234',
      },
      123,
      235,
    ],
  },
];
const config = { '^.*Code$': 'all' };
console.log(maskObject(sourceArr, config));

// 支持数组遍历,将所有带Code结尾的字段全掩码
// 输出:[ 123, { firstCode: '******', otherCode: [ { resultCode: '***', other: '234', }, 123, 235, ]}]

特殊数据类型

只会掩码numberstring这两种数据类型,其他数据类型会忽略

ts
import { maskObject } from 'maskio';

const sourceType = {
  strCode: '123456',
  numCode: 123,
  funcCode: () => {},
  arrCode: [1, 2],
  boolCode: true,
};
const config = { '^.*Code$': 'all' };
console.log(maskObject(sourceType, config));

// 输出:{strCode: '******',numCode: '***',funcCode: [Function: funcCode],arrCode: [ 1, 2 ],boolCode: true}