Giter VIP home page Giter VIP logo

easyformat's Introduction

Easy Format

简易常见的格式化小工具

安装

 npm i -S easy-fmt
 or
 yarn add easy-fmt

使用

import fmt from "easy-fmt";

let io = fmt.IntersectionObserver(document.getElementById("box", true));

io.then((entries, observe) => {
  if (entries[0].intersectionRatio <= 0) return;
  // 元素在可视区的处理逻辑
  observe.disconnect();
});

fmt.currency(12000); // 12,000.00

fmt.time(new Date(2020, 10, 22, 10, 15, 2), "YYYY-MM-DD hh:mm:ss"); // 2020-11-22 10:15:02

fmt.param({
  orderId: 15,
  orderNo: "Order20201022",
}); // ?orderId=15&orderNo=Order20201022

fmt.prefixZero(12, 5); // 00012
fmt.prefixZero(12, 1); // 12
fmt.prefixZero(-12, 3); // -012

fmt.round(12.456, 2); // 12.46

fmt.mask("15211112222", 12, 4); // 15211112222
fmt.mask("15211112222", 3, 4); // 152****2222

fmt.trim(" 152  111 222 ", "all"); // 152111222

fmt.phone("15211112222"); // 152-1111-2222
fmt.phone("15211112222", "/"); // 152/1111/2222
fmt.phone("15211112222", "-", "1/1/1"); // 1-5-2-11112222

API

方法 实例 备注
IntersectionObserver fmt.IntersectionObserver(el:Element, showOnce: Boolean) 监听指定元素是否在可视区域
currency fmt.currency(num: Number,placeholder: Boolean) 格式化货币
time `fmt.time(value: Number Date
param fmt.param(obj: Object) 将对象转换成 query
unparam fmt.unparam(val: String) 将 query 转换成对象
prefixZero fmt.prefixZero(num: Number, length: Number) 补零
round fmt.round(num: Number, decimals: Number) 四舍五入
randomNum fmt.randomNum(min: Number, max?: Number) 指定范围内的随机数
mask fmt.mask(str: String, start: Number, maskLen: Number, _placeholder: String) 字符串遮挡
maskLeft fmt.maskLeft(str: String, maskLen: Number, _placeholder: String) 从左侧开始遮挡
maskRight fmt.maskRight(str: String, maskLen: Number, _placeholder: String) 从右侧开始遮挡
trim fmt.trim(str: String, type: String['all', 'lr', 'left', 'right']) 清空字符串空格
guid fmt.guid() 生成随机码
phone fmt.phone(PhoneNumber: String, split: String, distribute: String) 格式化手机号码

easyformat's People

Watchers

James Cloos avatar wookyao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.