🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

nscutils

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nscutils - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
+3
.babelrc
{
"presets": ["es2015"]
}
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _utils = require('./utils');
var _utils2 = _interopRequireDefault(_utils);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = _utils2.default;
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/*
CDN:js.soufunimg.com/industry/commonjs/utils.min.js
*/
var u = function () {
function u() {
_classCallCheck(this, u);
}
_createClass(u, null, [{
key: 'isNumeric',
value: function isNumeric(obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj);
}
}, {
key: 'type',
value: function type(obj) {
return typeof obj === 'undefined' ? 'undefined' : _typeof(obj);
}
}, {
key: 'isObject',
value: function isObject(obj) {
return u.type(obj) == 'object';
}
}, {
key: 'isBoolean',
value: function isBoolean(obj) {
return u.type(obj) == 'boolean';
}
}]);
return u;
}();
/**
* @description 数组操作类
*/
var utilsArray = function () {
function utilsArray() {
_classCallCheck(this, utilsArray);
}
_createClass(utilsArray, null, [{
key: 'order',
/**
* @description 数组排序
* @example utils.Array.order([{value:10},{value:20}],'desc','value')
* @example utils.Array.order([10,20],'desc')
* @param {Array} array 要操作的数组
* @param {String} type 排序 asc/desc default asc
* @param {String} column 如果array是对象数组传字段名
* @returns {Array} 返回排序后的数组
*/
value: function order(array, type, column) {
var n1 = type == 'desc' ? -1 : 1,
n2 = n1 == 1 ? -1 : 1;
return array.sort(function (a, b) {
var _a = -Number.NEGATIVE_INFINITY,
_b = -Number.NEGATIVE_INFINITY,
av = u.isObject(a) ? a[column] : a,
bv = u.isObject(b) ? b[column] : b;
if (u.isNumeric(av)) _a = parseFloat(av);
if (u.isNumeric(bv)) _b = parseFloat(bv);
return _a > _b ? n1 : n2;
});
}
/**
* @description 数组去重
* @example utils.Array.unique([{name:'zs'},{name:'zs'}],'name')
* @example utils.Array.unique([10,10])
* @param {Array} array 要操作的数组
* @param {String} column 如果array是对象数组传字段名
* @returns {Array} 返回去重后的数组
*/
}, {
key: 'unique',
value: function unique(array, column) {
var result = [],
hash = {};
for (var i = 0; i < array.length; i++) {
var elem = _typeof(array[i]) == 'object' ? array[i][column] : array[i];
if (!hash[elem]) {
hash[elem] = true;
result.push(array[i]);
}
}
return result;
}
}]);
return utilsArray;
}();
/**
* @description 字符串操作类
*/
var utilsString = function () {
function utilsString() {
_classCallCheck(this, utilsString);
}
_createClass(utilsString, null, [{
key: 'removeHTML',
/**
* 剔除html标签
* @param {String} html 要剔除的html字符串
*/
value: function removeHTML(html) {
return html.replace(/<\/?[^>]*>/g, '').replace(/[ | ]*\n/g, '\n').replace(/&nbsp;/ig, '');
}
/**
* 字符串截断并加指定字符串 subString(`sadfsdafsd`,2,'...')
* @param {String} val 要截断的字符串
* @param {Number} len 长度
* @param {String} suffix 需要补齐的字符串
* @returns {String} sa...
*/
}, {
key: 'subString',
value: function subString(val, len, suffix) {
return (val || '').length > len ? val = val.substr(0, len) + (suffix == undefined ? '' : suffix) : val;
}
/**
* 字符串转换parseFloat并保留指定小数位
* @param {String} val 要操作的字符串
* @param {Number} len 保留小数位长度
* @param {Boolean} fill 如果小数位长度不足是否需要补齐 default =true
* @returns {Number} 转换后的值
*/
}, {
key: 'toFixed',
value: function toFixed(val, len, fill) {
fill = !u.isBoolean(fill) ? true : fill;
var v = val;
if (val && u.isNumeric(val)) {
if (fill) {
v = parseFloat(val).toFixed(len);
} else {
v = parseFloat(val);
if (val.toString().indexOf('.') > 0) {
var arr = val.toString().split('.');
v = arr[1].length > len ? v.toFixed(len) : v;
}
}
}
return v;
}
/**
* 删除前后空格
* @param {String} val 要操作的字符串
* @returns {Number} 删除前后空格后的字符串
*/
}, {
key: 'trim',
value: function trim(val) {
return val.replace(/(^\s*)|(\s*$)/g, '');
}
/**
* 替换所有
* @param {String} val 要操作的字符串
* @param {String} replacestr 被替换字符串
* @param {String} newstr 用来替换的字符串
* @returns {String} 替换后的字符串
*/
}, {
key: 'replaceAll',
value: function replaceAll(val, replacestr, newstr) {
return val.replace(new RegExp(replacestr, 'gm'), newstr);
}
/**
* 字符转换日期
* @param {String} val 要操作的日期字符串
* @param {String} split 日期字符串的连接方式,例'-'或'/'
* @returns {Date} 转换后日期对象
*/
}, {
key: 'toDate',
value: function toDate(val, split) {
split = split || '-';
var arr = val.split(split);
if (arr.length == 3) {
var date = new Date();
date.setUTCFullYear(arr[0], arr[1] - 1, arr[2]);
date.setUTCHours(0, 0, 0, 0);
return date;
}
}
}]);
return utilsString;
}();
/**
* @description Date操作类
*/
var utilsDate = function () {
function utilsDate() {
_classCallCheck(this, utilsDate);
}
_createClass(utilsDate, null, [{
key: 'format',
/**
* 日期格式化制定格式字符串
* @param {Date} date 日期
* @param {String} fmt 转换的格式 如 yyyy-MM-dd
*/
value: function format(date, fmt) {
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}return fmt;
}
}]);
return utilsDate;
}();
/**
* @description 对象操作类
*/
var utilsObject = function () {
function utilsObject() {
_classCallCheck(this, utilsObject);
}
_createClass(utilsObject, null, [{
key: 'extend',
/**
* 数组/对象 拷贝
*/
value: function extend() {
var src,
copyIsArray,
copy,
name,
options,
clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
if ((typeof target === 'undefined' ? 'undefined' : _typeof(target)) !== "object") {
target = {};
}
if (length === i) {
target = this;
--i;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging plain objects or arrays
if (deep && copy && (typeof copy === 'undefined' ? 'undefined' : _typeof(copy)) == 'object') {
copyIsArray = Array.isArray(copy);
if (copyIsArray) {
copyIsArray = false;
clone = src && Array.isArray(src) ? copy : [];
} else {
clone = src && (typeof src === 'undefined' ? 'undefined' : _typeof(src)) == 'object' ? src : {};
}
// Never move original objects, clone them
target[name] = utilsObject.extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
}
}]);
return utilsObject;
}();
/**
* @description 渐变色
*/
var Intensity = function () {
/**
* @constructor Intensity
* description 获取渐变色阶/size
* @param {Object} options
* @param {Object} options.gradient 色阶 {0.1:red,0.5:'#fff'}
* @param {Number} options.maxSize 最大size
* @param {Number} options.minSize 最小size
* @param {Number} options.max 最大值
* @param {String} options.colorType 返回颜色类型 'binary'||'rgb'
* @returns {Object} 当前对象实例
*/
function Intensity(options) {
_classCallCheck(this, Intensity);
options = options || {};
this.gradient = options.gradient || {
0.25: "rgba(0, 0, 255, 1)",
0.55: "rgba(0, 255, 0, 1)",
0.85: "rgba(255, 255, 0, 1)",
1.0: "rgba(255, 0, 0, 1)"
};
this.maxSize = options.maxSize || 35;
this.minSize = options.minSize || 0;
this.max = options.max || 100;
this.colorType = options.colorType || 'rgb';
this.initPalette();
}
_createClass(Intensity, [{
key: 'initPalette',
value: function initPalette() {
var gradient = this.gradient;
if (typeof document === 'undefined') {
// var Canvas = require('canvas');
// var paletteCanvas = new Canvas(256, 1);
} else {
var paletteCanvas = document.createElement('canvas');
}
paletteCanvas.width = 256;
paletteCanvas.height = 1;
var paletteCtx = this.paletteCtx = paletteCanvas.getContext('2d');
var lineGradient = paletteCtx.createLinearGradient(0, 0, 256, 1);
for (var key in gradient) {
lineGradient.addColorStop(parseFloat(key), gradient[key]);
}
paletteCtx.fillStyle = lineGradient;
paletteCtx.fillRect(0, 0, 256, 1);
}
/**
* 根据value获取色阶颜色
* @param {Number} value
* @returns {String}
*/
}, {
key: 'getColor',
value: function getColor(value) {
var max = this.max;
if (value > max) {
value = max;
}
var index = Math.floor(value / max * (256 - 1)) * 4;
var imageData = this.paletteCtx.getImageData(0, 0, 256, 1).data;
var color = "rgba(" + imageData[index] + ", " + imageData[index + 1] + ", " + imageData[index + 2] + ", " + imageData[index + 3] / 256 + ")";
return this.colorType == 'binary' ? utilsColor.rgb2binary(color) : color;
}
/**
* 根据value获取size
* @param {Number} value
*/
}, {
key: 'getSize',
value: function getSize(value) {
var size = 0;
var max = this.max;
var maxSize = this.maxSize;
var minSize = this.minSize;
if (value > max) {
value = max;
}
size = minSize + value / max * (maxSize - minSize);
return size;
}
}]);
return Intensity;
}();
/**
* @description 颜色转换
*/
var utilsColor = function () {
function utilsColor() {
_classCallCheck(this, utilsColor);
}
_createClass(utilsColor, null, [{
key: 'rgb2binary',
/**
* 根据rbg颜色转换成二进制网页色
* @param {String} color rgb颜色
* @returns {String} 二进制网页色
*/
value: function rgb2binary(color) {
color = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
return color && color.length === 4 ? '#' + ('0' + parseInt(color[1], 10).toString(16)).slice(-2) + ('0' + parseInt(color[2], 10).toString(16)).slice(-2) + ('0' + parseInt(color[3], 10).toString(16)).slice(-2) : '';
}
/**
*
* @param {Object} options
* @param {Object} options.gradient 色阶 {0.1:red,0.5:'#fff'}
* @param {Number} options.maxSize 最大size
* @param {Number} options.minSize 最小size
* @param {Number} options.max 最大值
* @param {String} options.colorType 返回颜色类型 'binary'||'rgb'
* @returns {Object} 当前对象实例
*/
}, {
key: 'intensity',
value: function intensity(options) {
return new Intensity(options);
}
}]);
return utilsColor;
}();
module.exports.Array = utilsArray;
module.exports.String = utilsString;
module.exports.Date = utilsDate;
module.exports.Object = utilsObject;
//获取色阶
module.exports.Color = utilsColor;
+13
-5
{
"name": "nscutils",
"version": "1.0.0",
"version": "1.0.1",
"description": "some utils",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"compile": "babel -d lib/ src/",
"prepublish": "npm run compile",
"server": "node app.babel.js",
"test": "echo \"Error: no test specified\" && exit 1"

@@ -11,3 +14,3 @@ },

"type": "git",
"url": "git+https://github.com/NorthSeacoder/npmPage.git"
"url": "https://github.com/NorthSeacoder/npmPage.git"
},

@@ -22,3 +25,8 @@ "keywords": [

},
"homepage": "https://github.com/NorthSeacoder/npmPage#readme"
}
"homepage": "https://github.com/NorthSeacoder/npmPage#readme",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1"
},
"dependencies": {}
}