大前端

npmString相关

2018-05-22  本文已影响16人  为了记笔记注册的账号

1.pad-left

左侧填充零或指定字符串的字符串

npm install pad-left --save

var pad = require('pad-left');
pad(  '4', 4, '0') // 0004
pad( '35', 4, '0') // 0035
pad('459', 4, '0') // 0459

2. decamelize

大写驼峰式转小写

npm install decamelize

const decamelize = require('decamelize');

decamelize('unicornRainbow');
//=> 'unicorn_rainbow'

decamelize('unicornRainbow', '-');
//=> 'unicorn-rainbow'

input

Type: string

separator

Type: string
Default: _

3.to-camel-case

将字符串装换为驼峰式

npm install to-camel-case

var toCamelCase = require('to-camel-case')

toCamelCase('space case')  // "spaceCase"
toCamelCase('snake_case')  // "snakeCase"
toCamelCase('dot.case')    // "dotCase"
toCamelCase('weird[case')  // "weirdCase"

4.to-capital-case

将字符串所有单词转换为首字母大写

npm install to-capital-case

var toCapitalCase = require('to-capital-case')

toCapitalCase('camelCase')        // "Camel Case"
toCapitalCase('space case')       // "Space Case"
toCapitalCase('snake_case')       // "Snake Case"
toCapitalCase('dot.case')         // "Dot Case"
toCapitalCase('some*weird[case')  // "Some Weird Case"

5.to-dot-case

将单词用 . 连接

npm install to-dot-case

var toDotCase = require('to-dot-case')

toDotCase('camelCase')   // "camel.case"
toDotCase('space case')  // "snake.case"
toDotCase('slug-case')   // "slug.case"
toDotCase('weird[case')  // "weird.case"

6.slice.js

将字符串或者数组剪切

npm install slice.js

// for array
const arr = slice([1, '2', 3, '4', 5, '6', 7, '8', 9, '0']);

arr[-2];        // 9
arr['2:5'];         // [3, '4', 5]
arr[':-2'];         // [1, '2', 3, '4', 5, '6', 7, '8']
arr['-2:'];         // [9, '0']
arr['1:5:2'];       // ['2', '4']
arr['5:1:-2'];      // ['6', '4']

// for string
const str = slice('1234567890');

str[-2];        // '9'
str['2:5'];         // '345'
str[':-2'];         // '12345678'
str['-2:'];         // '90'
str['1:5:2'];       // '24'
str['5:1:-2'];      // '64'
上一篇下一篇

猜你喜欢

热点阅读