acbceb950e69617cb8afde42d1e23dbc.json
26 KB
{"ast":null,"code":"/**\n * @fileoverview This module has some functions for handling object as collection.\n * @author NHN.\n * FE Development Lab <dl_javascript@nhn.com>\n */\n'use strict';\n\nvar type = require('./type');\n\nvar object = require('./object');\n/**\n * Execute the provided callback once for each element present\n * in the array(or Array-like object) in ascending order.<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the element\n * - The index of the element\n * - The array(or Array-like object) being traversed\n * @param {Array} arr The array(or Array-like object) that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEachArray([1,2,3], function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n */\n\n\nfunction forEachArray(arr, iteratee, context) {\n var index = 0;\n var len = arr.length;\n context = context || null;\n\n for (; index < len; index += 1) {\n if (iteratee.call(context, arr[index], index, arr) === false) {\n break;\n }\n }\n}\n/**\n * Execute the provided callback once for each property of object which actually exist.<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property\n * - The name of the property\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEachOwnProperties({a:1,b:2,c:3}, function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n **/\n\n\nfunction forEachOwnProperties(obj, iteratee, context) {\n var key;\n context = context || null;\n\n for (key in obj) {\n if (obj.hasOwnProperty(key)) {\n if (iteratee.call(context, obj[key], key, obj) === false) {\n break;\n }\n }\n }\n}\n/**\n * Execute the provided callback once for each property of object(or element of array) which actually exist.<br>\n * If the object is Array-like object(ex-arguments object), It needs to transform to Array.(see 'ex2' of example).<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEach([1,2,3], function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n *\n * // In case of Array-like object\n * var array = Array.prototype.slice.call(arrayLike); // change to array\n * util.forEach(array, function(value){\n * sum += value;\n * });\n */\n\n\nfunction forEach(obj, iteratee, context) {\n if (type.isArray(obj)) {\n forEachArray(obj, iteratee, context);\n } else {\n forEachOwnProperties(obj, iteratee, context);\n }\n}\n/**\n * Execute the provided callback function once for each element in an array, in order,\n * and constructs a new array from the results.<br>\n * If the object is Array-like object(ex-arguments object),\n * It needs to transform to Array.(see 'ex2' of forEach example)<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {Array} A new array composed of returned values from callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result = util.map([0,1,2,3], function(value) {\n * return value + 1;\n * });\n *\n * alert(result); // 1,2,3,4\n */\n\n\nfunction map(obj, iteratee, context) {\n var resultArray = [];\n context = context || null;\n forEach(obj, function () {\n resultArray.push(iteratee.apply(context, arguments));\n });\n return resultArray;\n}\n/**\n * Execute the callback function once for each element present in the array(or Array-like object or plain object).<br>\n * If the object is Array-like object(ex-arguments object),\n * It needs to transform to Array.(see 'ex2' of forEach example)<br>\n * Callback function(iteratee) is invoked with four arguments:\n * - The previousValue\n * - The currentValue\n * - The index\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {*} The result value\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result = util.reduce([0,1,2,3], function(stored, value) {\n * return stored + value;\n * });\n *\n * alert(result); // 6\n */\n\n\nfunction reduce(obj, iteratee, context) {\n var index = 0;\n var keys, length, store;\n context = context || null;\n\n if (!type.isArray(obj)) {\n keys = object.keys(obj);\n length = keys.length;\n store = obj[keys[index += 1]];\n } else {\n length = obj.length;\n store = obj[index];\n }\n\n index += 1;\n\n for (; index < length; index += 1) {\n store = iteratee.call(context, store, obj[keys ? keys[index] : index]);\n }\n\n return store;\n}\n/**\n * Transform the Array-like object to Array.<br>\n * In low IE (below 8), Array.prototype.slice.call is not perfect. So, try-catch statement is used.\n * @param {*} arrayLike Array-like object\n * @returns {Array} Array\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var arrayLike = {\n * 0: 'one',\n * 1: 'two',\n * 2: 'three',\n * 3: 'four',\n * length: 4\n * };\n * var result = util.toArray(arrayLike);\n *\n * alert(result instanceof Array); // true\n * alert(result); // one,two,three,four\n */\n\n\nfunction toArray(arrayLike) {\n var arr;\n\n try {\n arr = Array.prototype.slice.call(arrayLike);\n } catch (e) {\n arr = [];\n forEachArray(arrayLike, function (value) {\n arr.push(value);\n });\n }\n\n return arr;\n}\n/**\n * Create a new array or plain object with all elements(or properties)\n * that pass the test implemented by the provided function.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj Object(plain object or Array) that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {Object} plain object or Array\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result1 = util.filter([0,1,2,3], function(value) {\n * return (value % 2 === 0);\n * });\n * alert(result1); // [0, 2]\n *\n * var result2 = util.filter({a : 1, b: 2, c: 3}, function(value) {\n * return (value % 2 !== 0);\n * });\n * alert(result2.a); // 1\n * alert(result2.b); // undefined\n * alert(result2.c); // 3\n */\n\n\nfunction filter(obj, iteratee, context) {\n var result, add;\n context = context || null;\n\n if (!type.isObject(obj) || !type.isFunction(iteratee)) {\n throw new Error('wrong parameter');\n }\n\n if (type.isArray(obj)) {\n result = [];\n\n add = function (subResult, args) {\n subResult.push(args[0]);\n };\n } else {\n result = {};\n\n add = function (subResult, args) {\n subResult[args[1]] = args[0];\n };\n }\n\n forEach(obj, function () {\n if (iteratee.apply(context, arguments)) {\n add(result, arguments);\n }\n }, context);\n return result;\n}\n/**\n * fetching a property\n * @param {Array} arr target collection\n * @param {String|Number} property property name\n * @returns {Array}\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var objArr = [\n * {'abc': 1, 'def': 2, 'ghi': 3},\n * {'abc': 4, 'def': 5, 'ghi': 6},\n * {'abc': 7, 'def': 8, 'ghi': 9}\n * ];\n * var arr2d = [\n * [1, 2, 3],\n * [4, 5, 6],\n * [7, 8, 9]\n * ];\n * util.pluck(objArr, 'abc'); // [1, 4, 7]\n * util.pluck(arr2d, 2); // [3, 6, 9]\n */\n\n\nfunction pluck(arr, property) {\n var result = map(arr, function (item) {\n return item[property];\n });\n return result;\n}\n\nmodule.exports = {\n forEachOwnProperties: forEachOwnProperties,\n forEachArray: forEachArray,\n forEach: forEach,\n toArray: toArray,\n map: map,\n reduce: reduce,\n filter: filter,\n pluck: pluck\n};","map":{"version":3,"sources":["C:/Users/kkwan_000/Desktop/git/2017110269/minsung/node_modules/tui-code-snippet/src/js/collection.js"],"names":["type","require","object","forEachArray","arr","iteratee","context","index","len","length","call","forEachOwnProperties","obj","key","hasOwnProperty","forEach","isArray","map","resultArray","push","apply","arguments","reduce","keys","store","toArray","arrayLike","Array","prototype","slice","e","value","filter","result","add","isObject","isFunction","Error","subResult","args","pluck","property","item","module","exports"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AAEA;;AAEA,IAAIA,IAAI,GAAGC,OAAO,CAAC,QAAD,CAAlB;;AACA,IAAIC,MAAM,GAAGD,OAAO,CAAC,UAAD,CAApB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASE,YAAT,CAAsBC,GAAtB,EAA2BC,QAA3B,EAAqCC,OAArC,EAA8C;AAC1C,MAAIC,KAAK,GAAG,CAAZ;AACA,MAAIC,GAAG,GAAGJ,GAAG,CAACK,MAAd;AAEAH,EAAAA,OAAO,GAAGA,OAAO,IAAI,IAArB;;AAEA,SAAOC,KAAK,GAAGC,GAAf,EAAoBD,KAAK,IAAI,CAA7B,EAAgC;AAC5B,QAAIF,QAAQ,CAACK,IAAT,CAAcJ,OAAd,EAAuBF,GAAG,CAACG,KAAD,CAA1B,EAAmCA,KAAnC,EAA0CH,GAA1C,MAAmD,KAAvD,EAA8D;AAC1D;AACH;AACJ;AACJ;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASO,oBAAT,CAA8B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,IAAI,CAAC,CAAD,CAAzB;AACH,KAFD;AAGH;;AAEDxB,EAAAA,OAAO,CAACH,GAAD,EAAM,YAAW;AACpB,QAAIP,QAAQ,CAACe,KAAT,CAAed,OAAf,EAAwBe,SAAxB,CAAJ,EAAwC;AACpCa,MAAAA,GAAG,CAACD,MAAD,EAASZ,SAAT,CAAH;AACH;AACJ,GAJM,EAIJf,OAJI,CAAP;AAMA,SAAO2B,MAAP;AACH;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASO,KAAT,CAAepC,GAAf,EAAoBqC,QAApB,EAA8B;AAC1B,MAAIR,MAAM,GAAGhB,GAAG,CAACb,GAAD,EAAM,UAASsC,IAAT,EAAe;AACjC,WAAOA,IAAI,CAACD,QAAD,CAAX;AACH,GAFe,CAAhB;AAIA,SAAOR,MAAP;AACH;;AAEDU,MAAM,CAACC,OAAP,GAAiB;AACbjC,EAAAA,oBAAoB,EAAEA,oBADT;AAEbR,EAAAA,YAAY,EAAEA,YAFD;AAGbY,EAAAA,OAAO,EAAEA,OAHI;AAIbU,EAAAA,OAAO,EAAEA,OAJI;AAKbR,EAAAA,GAAG,EAAEA,GALQ;AAMbK,EAAAA,MAAM,EAAEA,MANK;AAObU,EAAAA,MAAM,EAAEA,MAPK;AAQbQ,EAAAA,KAAK,EAAEA;AARM,CAAjB","sourcesContent":["/**\n * @fileoverview This module has some functions for handling object as collection.\n * @author NHN.\n * FE Development Lab <dl_javascript@nhn.com>\n */\n\n'use strict';\n\nvar type = require('./type');\nvar object = require('./object');\n\n/**\n * Execute the provided callback once for each element present\n * in the array(or Array-like object) in ascending order.<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the element\n * - The index of the element\n * - The array(or Array-like object) being traversed\n * @param {Array} arr The array(or Array-like object) that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEachArray([1,2,3], function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n */\nfunction forEachArray(arr, iteratee, context) {\n var index = 0;\n var len = arr.length;\n\n context = context || null;\n\n for (; index < len; index += 1) {\n if (iteratee.call(context, arr[index], index, arr) === false) {\n break;\n }\n }\n}\n\n/**\n * Execute the provided callback once for each property of object which actually exist.<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property\n * - The name of the property\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEachOwnProperties({a:1,b:2,c:3}, function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n **/\nfunction forEachOwnProperties(obj, iteratee, context) {\n var key;\n\n context = context || null;\n\n for (key in obj) {\n if (obj.hasOwnProperty(key)) {\n if (iteratee.call(context, obj[key], key, obj) === false) {\n break;\n }\n }\n }\n}\n\n/**\n * Execute the provided callback once for each property of object(or element of array) which actually exist.<br>\n * If the object is Array-like object(ex-arguments object), It needs to transform to Array.(see 'ex2' of example).<br>\n * If the callback function returns false, the loop will be stopped.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var sum = 0;\n *\n * util.forEach([1,2,3], function(value){\n * sum += value;\n * });\n * alert(sum); // 6\n *\n * // In case of Array-like object\n * var array = Array.prototype.slice.call(arrayLike); // change to array\n * util.forEach(array, function(value){\n * sum += value;\n * });\n */\nfunction forEach(obj, iteratee, context) {\n if (type.isArray(obj)) {\n forEachArray(obj, iteratee, context);\n } else {\n forEachOwnProperties(obj, iteratee, context);\n }\n}\n\n/**\n * Execute the provided callback function once for each element in an array, in order,\n * and constructs a new array from the results.<br>\n * If the object is Array-like object(ex-arguments object),\n * It needs to transform to Array.(see 'ex2' of forEach example)<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {Array} A new array composed of returned values from callback function\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result = util.map([0,1,2,3], function(value) {\n * return value + 1;\n * });\n *\n * alert(result); // 1,2,3,4\n */\nfunction map(obj, iteratee, context) {\n var resultArray = [];\n\n context = context || null;\n\n forEach(obj, function() {\n resultArray.push(iteratee.apply(context, arguments));\n });\n\n return resultArray;\n}\n\n/**\n * Execute the callback function once for each element present in the array(or Array-like object or plain object).<br>\n * If the object is Array-like object(ex-arguments object),\n * It needs to transform to Array.(see 'ex2' of forEach example)<br>\n * Callback function(iteratee) is invoked with four arguments:\n * - The previousValue\n * - The currentValue\n * - The index\n * - The object being traversed\n * @param {Object} obj The object that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {*} The result value\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result = util.reduce([0,1,2,3], function(stored, value) {\n * return stored + value;\n * });\n *\n * alert(result); // 6\n */\nfunction reduce(obj, iteratee, context) {\n var index = 0;\n var keys, length, store;\n\n context = context || null;\n\n if (!type.isArray(obj)) {\n keys = object.keys(obj);\n length = keys.length;\n store = obj[keys[index += 1]];\n } else {\n length = obj.length;\n store = obj[index];\n }\n\n index += 1;\n for (; index < length; index += 1) {\n store = iteratee.call(context, store, obj[keys ? keys[index] : index]);\n }\n\n return store;\n}\n\n/**\n * Transform the Array-like object to Array.<br>\n * In low IE (below 8), Array.prototype.slice.call is not perfect. So, try-catch statement is used.\n * @param {*} arrayLike Array-like object\n * @returns {Array} Array\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var arrayLike = {\n * 0: 'one',\n * 1: 'two',\n * 2: 'three',\n * 3: 'four',\n * length: 4\n * };\n * var result = util.toArray(arrayLike);\n *\n * alert(result instanceof Array); // true\n * alert(result); // one,two,three,four\n */\nfunction toArray(arrayLike) {\n var arr;\n try {\n arr = Array.prototype.slice.call(arrayLike);\n } catch (e) {\n arr = [];\n forEachArray(arrayLike, function(value) {\n arr.push(value);\n });\n }\n\n return arr;\n}\n\n/**\n * Create a new array or plain object with all elements(or properties)\n * that pass the test implemented by the provided function.<br>\n * Callback function(iteratee) is invoked with three arguments:\n * - The value of the property(or The value of the element)\n * - The name of the property(or The index of the element)\n * - The object being traversed\n * @param {Object} obj Object(plain object or Array) that will be traversed\n * @param {function} iteratee Callback function\n * @param {Object} [context] Context(this) of callback function\n * @returns {Object} plain object or Array\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var result1 = util.filter([0,1,2,3], function(value) {\n * return (value % 2 === 0);\n * });\n * alert(result1); // [0, 2]\n *\n * var result2 = util.filter({a : 1, b: 2, c: 3}, function(value) {\n * return (value % 2 !== 0);\n * });\n * alert(result2.a); // 1\n * alert(result2.b); // undefined\n * alert(result2.c); // 3\n */\nfunction filter(obj, iteratee, context) {\n var result, add;\n\n context = context || null;\n\n if (!type.isObject(obj) || !type.isFunction(iteratee)) {\n throw new Error('wrong parameter');\n }\n\n if (type.isArray(obj)) {\n result = [];\n add = function(subResult, args) {\n subResult.push(args[0]);\n };\n } else {\n result = {};\n add = function(subResult, args) {\n subResult[args[1]] = args[0];\n };\n }\n\n forEach(obj, function() {\n if (iteratee.apply(context, arguments)) {\n add(result, arguments);\n }\n }, context);\n\n return result;\n}\n\n/**\n * fetching a property\n * @param {Array} arr target collection\n * @param {String|Number} property property name\n * @returns {Array}\n * @memberof tui.util\n * @example\n * //-- #1. Get Module --//\n * var util = require('tui-code-snippet'); // node, commonjs\n * var util = tui.util; // distribution file\n *\n * //-- #2. Use property --//\n * var objArr = [\n * {'abc': 1, 'def': 2, 'ghi': 3},\n * {'abc': 4, 'def': 5, 'ghi': 6},\n * {'abc': 7, 'def': 8, 'ghi': 9}\n * ];\n * var arr2d = [\n * [1, 2, 3],\n * [4, 5, 6],\n * [7, 8, 9]\n * ];\n * util.pluck(objArr, 'abc'); // [1, 4, 7]\n * util.pluck(arr2d, 2); // [3, 6, 9]\n */\nfunction pluck(arr, property) {\n var result = map(arr, function(item) {\n return item[property];\n });\n\n return result;\n}\n\nmodule.exports = {\n forEachOwnProperties: forEachOwnProperties,\n forEachArray: forEachArray,\n forEach: forEach,\n toArray: toArray,\n map: map,\n reduce: reduce,\n filter: filter,\n pluck: pluck\n};\n"]},"metadata":{},"sourceType":"script"}