UNPKG

apollo-nico

Version:

对 nico 及 apollo-theme 的封装,方便跨平台使用

275 lines (238 loc) 5.78 kB
var toString = Object.prototype.toString, slice = Array.prototype.slice, // Inherit from native utility. util = Object.create(require('util')), /** * Format printf-like string. * @param template {string} * @param value {**} * @return {string} */ format = util.format, /** * Test whether type of input value is Array. * @param value {*} * @return {boolean} */ isArray = util.isArray, /** * Test whether type of input value is Boolean. * @param value {*} * @return {boolean} */ isBoolean = util.isBoolean = function (value) { return typeof value === 'boolean'; }, /** * Test whether type of input value is Date. * @param value {*} * @return {boolean} */ isDate = util.isDate, /** * Test whether type of input value is Error. * @param value {*} * @return {boolean} */ isError = util.isError, /** * Test whether type of input value is Function. * @param value {*} * @return {boolean} */ isFunction = util.isFunction = function (value) { return typeof value === 'function'; }, /** * Test whether type of input value is Null. * @param value {*} * @return {boolean} */ isNull = util.isNull = function (value) { return value === null; }, /** * Test whether type of input value is Number. * @param value {*} * @return {boolean} */ isNumber = util.isNumber = function (value) { return typeof value === 'number' && isFinite(value); }, /** * Test whether type of input value is Object. * @param value {*} * @return {boolean} */ isObject = util.isObject = function (value) { return value === Object(value); }, /** * Test whether type of input value is RegExp. * @param value {*} * @return {boolean} */ isRegExp = util.isRegExp, /** * Test whether type of input value is String. * @param value {*} * @return {boolean} */ isString = util.isString = function (value) { return typeof value === 'string'; }, /** * Test whether type of input value is Undefined. * @param value {*} * @return {boolean} */ isUndefined = util.isUndefined = function(value) { return typeof value === 'undefined'; }, /** * Convert array-like object to array. * @param value {*} * @return {boolean} */ toArray = util.toArray = function (obj) { return slice.call(obj); }, /** * Get type of input value. * Copyright 2011 Yahoo! Inc. All rights reserved. * @param value {*} * @return {string} */ type = util.type = (function () { var TYPES = { 'undefined' : 'undefined', 'number' : 'number', 'boolean' : 'boolean', 'string' : 'string', '[object Function]': 'function', '[object RegExp]' : 'regexp', '[object Array]' : 'array', '[object Date]' : 'date', '[object Error]' : 'error' }; return function (value) { return TYPES[typeof value] || TYPES[toString.call(value)] || (value ? 'object' : 'null'); }; }()), /** * Create a child constructor. * @param parent {Function} * @param prototype {Object} * @return {Function} */ inherit = util.inherit = function (parent, prototype) { var child = function () { if (isFunction(this._initialize)) { this._initialize.apply(this, arguments); } }; prototype.__proto__ = parent.prototype; // Mimic behavior of native Function.prototype. Object.defineProperty(prototype, 'constructor', { enumerable: false, value: child }); child.prototype = prototype; child.superclass = parent.prototype; child.extend = inherit.bind(null, child); return child; }, /** * Shallow copy properties from souce object to target object. * @param target {Object} * @param source {Object+} * @param [override=true] {boolean} * @return {Object} */ mix = util.mix = function () { var args = toArray(arguments), target = args[0] || {}, overwrite = isBoolean(args[args.length - 1]) ? args.pop() : true, i, len1, source, keys, j, len2, key; for (i = 1, len1 = args.length; i < len1; ++i) { source = args[i]; if (source) { keys = Object.keys(source); for (j = 0, len2 = keys.length; j < len2; ++j) { key = keys[j]; if (!target.hasOwnProperty(key) || overwrite) { target[key] = source[key]; } } } } return target; }, /** * Shallow copy properties from souce object to new-created empty object. * @param source {Object+} * @return {Object} */ merge = util.merge = function () { var args = toArray(arguments); args.unshift({}); return mix.apply(util, args); }, /** * Iterate key-value pair in object or array. * @param obj {Object} * @param iterator {Function} * @param context {Object} */ each = util.each = function (obj, iterator, context) { keys(obj).forEach(function (key) { iterator.call(context || null, obj[key], key, obj); }); }, /** * Remove duplicate array items. * @param arr {Array} * @return {Array} */ unique = util.unique = function (arr) { return arr.filter(function (value, index, arr) { return arr.indexOf(value) === index; }); }, /** * Get keys of an object. * @param obj {Object} * @return {Array} */ keys = util.keys = Object.keys, /** * Get values of an object. * @param obj {Object} * @return {Array} */ values = util.values = function (obj) { var result = []; each(obj, function (value) { result.push(value); }); return result; }, /** * Cache error message. * @param pattern {string} * @param [value] {string} * @return {Object|undefined} */ error = util.error = (function () { var cache = []; return function () { if (arguments.length === 0) { return cache; } else { cache.push(format.apply(null, arguments)); } }; }()); module.exports = util;