@soei/format

1.0.4 • Public • Published

工具类 format

StringMap 的基本用法

// 引入方式
const soei = require("@soei/format");
let StringMap = soei.StringMap;
// 或
import { StringMap } from "@soei/format";
let NM = new StringMap('name,age,sex,height'/* , '|' // 默认'/' */);

NM.toString({
    name: 'Tom',
    age: 3
})
// Tom/3//
NM.toString({
    height: '120cm'
})
// Tom/3//120cm
NM.data()
// { name: 'Tom', age: '3', sex: '', height: '120cm' }
NM.data('jerry/13//130cm')
// { name: 'jerry', age: '13', sex: '', height: '120cm' }

format 的基本用法

// 引入方式
const soei = require("@soei/format");
let format = soei.format;
// 或
import { format } from "@soei/format";
  • format('{attr}', args1 );
  • format('? ? ? ? ', args1, args2, ... );
format("name: ? age: ? color: ? from: ?", "tom", "2", "blue", "小漂亮国");
// 输出: 'name: tom age: 2 color: blue from: 小漂亮国'
format("name: ? age: ? color: ? from: ?", "未知");
// 输出: 'name: 未知 age: 未知 color: 未知 from: 未知'
format("{name} 是个男孩.", { name: "小明" });
// '小明是个男孩.'
format("name: ? age: ? color: ? from: ?", {
  name: "tom",
  age: "2",
  color: "blue",
  from: "小漂亮国",
});
// 输出: 'name: tom age: 2 color: blue from: 小漂亮国'

format 中的随机用法

// '...'.['format', 'on'];
format("[red,green,blue,yellow]");
// 随机输出: 'green'

format 中的 三元运算 用法 {expr,true,false}

format("{name,name,age}", {
  name: "",
  age: 1,
});
// 输出: 1
format("{name>2,+name + 2, +age - 1}", {
  name: "3",
  age: 1,
});
// '5'
format("{name>2,+name + 2, +age - 1}", {
  name: "2",
  age: 1,
});
// '0'

format 中的 区间随机 用法

format("{99-999}");
// 随机输出: '610'
format("{100-999}-{100-999}-{100-999}-{100-999}");
// 输出: '298-768-285-212'

format 中的 属性 用法

format("{now}");
// 输出: 当前毫秒值
format("{fx} is in!", {
  fx: () => {
    return "string";
  }
});
// 'string is in!'
format("{fx} is in!", {
  fx: "string"
});
// 'string is in!'

Package Sidebar

Install

npm i @soei/format

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

11.2 kB

Total Files

5

Last publish

Collaborators

  • soeiz