Giter Club home page Giter Club logo

any.prototype's Introduction

any.prototype

这是一个 JavaScript 变量的属性和方法拓展库。为防止代码入侵,在每个属性和方法的名字前加上一个 $ 符号。


安装

yarn add any.prototype

or

npm install any.prototype

导入

你可以全部导入

import 'any.prototype'

也可以单个类型导入

import 'any.prototype/String.prototype'

Array

判断数组中是否包含该元素

array.$have(element)
const array = [1, 2, 3]
const value = array.$have(0)
console.log(value) // false

删除数组中指定索引的元素,返回被删除的元素值

array.$remove(index)
const array = [1, 2, 3]
const value = array.$remove(0)
console.log(value) // 1
console.log(array) // [2, 3]

获取数组中第一个元素

array.$first
const array = [1, 2, 3]
const value = array.$first
console.log(value) // 1

获取数组中最后一个元素

array.$last
const array = [1, 2, 3]
const value = array.$last
console.log(value) // 3

Date

时间格式化,返回字符串

date.$format(formatter)
const date = new Date()
const value = date.$format('yyyy-MM-dd HH:mm:ss S')
console.log(value) // 2021-01-06 13:52:10 675

根据时间戳时间格式化

// 无论时间戳是秒还是毫秒,内部都会转换为毫秒,所以外部不用再去做转换
const timestamp = '1609900369647'

// 只传时间戳,返回的是 Date 类型的对象
const date = Date.$init(timestamp)

// 传时间戳和格式,返回的是对应格式的字符串数据
const date = Date.$init(timestamp, 'HH:mm:ss')

// 传时间戳和空格式,返回的是对应标准格式的字符串数据
const date = Date.$init(timestamp, '')

获取当前的时间戳

// 不传参数或传 's',返回的是以秒为单位的时间戳
Date.$time()
Date.$time('s')

// 传 'ms',返回的是以毫秒为单位的时间戳
Date.$time('ms')

Promise

多个 Promise 同时一起进行,最先成功的返回 resolve,都失败了返回 reject,可以参考 Promise.race()

Promise.$heap(array)
const api1 = _ => new Promise()
const api2 = _ => new Promise()
const api3 = _ => new Promise()
const array = [api1, api2, api3]
Promise.$heap(array)

多个 Promise 依次进行,若有一个成功的返回 resolve,最后一个也失败了返回 reject,可以参考 Promise.race()

Promise.$queue(array)
const api1 = _ => new Promise()
const api2 = _ => new Promise()
const api3 = _ => new Promise()
const array = [api1, api2, api3]
Promise.$queue(array)

String

字符串格式化

'{} {}!'.format('Hello', 'World') // 'Hello World!'

字符串格式化,也可以在最后一个参数传递被函数包裹的自定义格式符

'%@ %@!'.format('Hello', 'World', _ => '%@') // 'Hello World!'

字符串转换HTML转义符, 防代码注入

'<p>hello<p>'.$replaceHTMLChar() // '&lt;p&gt;hello&lt;p&gt;'

字符串前后加上单引号,在使用数据库增删改查时可能用到

'hello'.$quote // "'hello'"

字符串去除前后的空白字符

' hel lo  '.trimm // 'hel lo'


any.prototype's People

Contributors

mr-huangjian avatar

Watchers

 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.