UNPKG

native-class-ext

Version:

js原生对象增加常用方法

62 lines (57 loc) 1.55 kB
(function(){ const defineProperty = require('./defineProperty.js') /** * 对String类进行扩展 * @author gwl * @since 2020年5月8日22:06:27 * 扩展方法: * */ function init(){ /** * 判断字符串是否为空 * @param {String} str 值 * @param {Number} minLength 最小长度,默认1 * */ defineProperty(String, '$isEmpty', isEmpty) defineProperty(String.prototype, '$isEmpty', isEmptyp) /** * 指定位置插入字符串 * @param {Number} start 指定位置,0开始 * @param {String} str 要插入的字符串 * */ defineProperty(String.prototype, '$insertStr', insertStr) if(typeof String.prototype.padEnd != 'function') { defineProperty(String.prototype, 'padEnd', padEnd) } if(typeof String.prototype.padStart != 'function') { defineProperty(String.prototype, 'padStart', padStart) } } function isEmpty(str, minLength=1){ return !str || str.toString().trim().length<minLength; } function isEmptyp(minLength=1){ return this.trim().length<minLength; } function insertStr(start, str){ if(!str) return this; return this.substring(0, start) + str + this.substring(start) } function padEnd(len, str=' '){ if(!str || str.length==0) return; let temp = this; while(temp.length < len) { temp += str; } return temp; } function padStart(len, str=' '){ if(!str || str.length == 0) return; let temp = this; while(temp.length < len) { temp = str + temp; } return temp; } init(); })()