php-trim-plus

php string trim function in javascript.

Usage no npm install needed!

<script type="module">
  import phpTrimPlus from 'https://cdn.skypack.dev/php-trim-plus';
</script>

README

php-trim-plus - PHP风格的trim函数加强版

Npm version Build Status Dependencies Status

PHP的trim比较好用,但默认trim如果指定了charList,则只截取charList中的文本,默认的空格(换行字符等)就不处理了,修改了次模式,默认为追加模式,即whitespace + charList。

安装说明

npm install php-trim-plus --save
// or
yarn add php-trim-plus

使用说明

默认打包的版本(index.js),已经将依赖的 lodash 整合打包。

trim.js 未打包 lodash

const {trim, ltrim, rtrim, toSafeString, isSymbol, isString, isEmptyString, isEmptyStringOrWhitespace} = require('../php-trim-plus');
// or
import {trim, ltrim, rtrim, toSafeString, isSymbol, isString, isEmptyString, isEmptyStringOrWhitespace} from 'php-trim-plus';
trim(str, charList, isPlus)
ltrim(str, charList, isPlus)
rtrim(str, charList, isPlus)
trim(' 开源中国 '); // '开源中国'
trim(' 红薯-- ', '-'); // '红薯'

str: string 要截取的字符串 charList: string 要额外截取的字符串 isPlus: boolean 对 charList 是在现有空字符的基础上追加 charList,默认为 true,trim.Replace or trim.Plus

toSafeString(value)

toSafeString(['a', 'b', 'c'], '/'); // 'a/b/c'

将字符串转为安全字符串。

如果 value 为数组类型,会将数组打扁 flattenDeep 后,再join,可以指定第二参数 spr

字符串转换,默认增加了 value.normalize() 转换 unicode。

isString(value); 

lodash.isString 的引用

isEmptyString(value);

isEmptyString(null); // true
isEmptyString(undefined); // true
isEmptyString(''); // true
isEmptyString([]); // false
isEmptyString({}); // false

是否为空白字符串,这里不会对 value 进行trim,如果需要检查是否为纯粹的空格,请使用 isEmptyStringOrWhitespace

isEmptyStringOrWhitespace(value);

isEmptyStringOrWhitespace(null); // true
isEmptyStringOrWhitespace(undefined); // true
isEmptyStringOrWhitespace(''); // true
isEmptyStringOrWhitespace(' '); // true
isEmptyStringOrWhitespace('\t'); // true
isEmptyStringOrWhitespace('\n'); // true
isEmptyStringOrWhitespace([]); // false
isEmptyStringOrWhitespace({}); // false

是否为空白字符串或者是只包含空格符号的空白字符串,这里对 value 进行trim。

isSymbol(value); 

判断值是否为Symbol