├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | Inspired by the [collections/map][mout] util in mout. 2 | 3 | ## Usage 4 | 5 | ```js 6 | var map = require('{%= name %}'); 7 | ``` 8 | 9 | ### objects 10 | 11 | ```js 12 | var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) { 13 | return item; 14 | }); 15 | console.log(res); 16 | // => ['foo', 'bar', 'baz'] 17 | 18 | var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) { 19 | return key; 20 | }); 21 | console.log(res); 22 | // => ['a', 'b', 'c'] 23 | ``` 24 | 25 | ### arrays 26 | 27 | ```js 28 | var res = map(['foo', 'bar', 'baz'], function(item, index, array) { 29 | return item; 30 | }); 31 | console.log(res); 32 | // => ['foo', 'bar', 'baz'] 33 | 34 | var res = map(['foo', 'bar', 'baz'], function(item, index, array) { 35 | return index; 36 | }); 37 | console.log(res); 38 | // => [0, 1, 2] 39 | ``` 40 | 41 | ### strings 42 | 43 | A string may be passed as the second argument, for getting properties: 44 | 45 | ```js 46 | var obj = { 47 | a: {aaa: 'one', bbb: 'four', ccc: 'seven'}, 48 | b: {aaa: 'two', bbb: 'five', ccc: 'eight'}, 49 | c: {aaa: 'three', bbb: 'six', ccc: 'nine'} 50 | }; 51 | 52 | console.log(map(obj, 'aaa')); 53 | // => ['one', 'two', 'three'] 54 | 55 | var array = [obj.a, obj.b, obj.c]; 56 | console.log(map(array, 'bbb')); 57 | // => ['four', 'five', 'six'] 58 | ``` 59 | 60 | ### thisArg 61 | 62 | Invocation context may be passed as the last argument. 63 | 64 | ```js 65 | var array = ['a', 'b', 'c']; 66 | var ctx = {a: 'aaa', b: 'bbb', c: 'ccc'}; 67 | 68 | var res = map(array, function(item, index, array) { 69 | return this[item]; 70 | }, ctx); 71 | 72 | console.log(res); 73 | // => ['aaa', 'bbb', 'ccc'] 74 | ``` 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, 2017, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # collection-map [![NPM version](https://img.shields.io/npm/v/collection-map.svg?style=flat)](https://www.npmjs.com/package/collection-map) [![NPM monthly downloads](https://img.shields.io/npm/dm/collection-map.svg?style=flat)](https://npmjs.org/package/collection-map) [![NPM total downloads](https://img.shields.io/npm/dt/collection-map.svg?style=flat)](https://npmjs.org/package/collection-map) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/collection-map.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/collection-map) 2 | 3 | > Returns an array of mapped values from an array or object. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save collection-map 11 | ``` 12 | 13 | Inspired by the [collections/map](http://moutjs.com/) util in mout. 14 | 15 | ## Usage 16 | 17 | ```js 18 | var map = require('collection-map'); 19 | ``` 20 | 21 | ### objects 22 | 23 | ```js 24 | var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) { 25 | return item; 26 | }); 27 | console.log(res); 28 | // => ['foo', 'bar', 'baz'] 29 | 30 | var res = map({a: 'foo', b: 'bar', c: 'baz'}, function(item, key, obj) { 31 | return key; 32 | }); 33 | console.log(res); 34 | // => ['a', 'b', 'c'] 35 | ``` 36 | 37 | ### arrays 38 | 39 | ```js 40 | var res = map(['foo', 'bar', 'baz'], function(item, index, array) { 41 | return item; 42 | }); 43 | console.log(res); 44 | // => ['foo', 'bar', 'baz'] 45 | 46 | var res = map(['foo', 'bar', 'baz'], function(item, index, array) { 47 | return index; 48 | }); 49 | console.log(res); 50 | // => [0, 1, 2] 51 | ``` 52 | 53 | ### strings 54 | 55 | A string may be passed as the second argument, for getting properties: 56 | 57 | ```js 58 | var obj = { 59 | a: {aaa: 'one', bbb: 'four', ccc: 'seven'}, 60 | b: {aaa: 'two', bbb: 'five', ccc: 'eight'}, 61 | c: {aaa: 'three', bbb: 'six', ccc: 'nine'} 62 | }; 63 | 64 | console.log(map(obj, 'aaa')); 65 | // => ['one', 'two', 'three'] 66 | 67 | var array = [obj.a, obj.b, obj.c]; 68 | console.log(map(array, 'bbb')); 69 | // => ['four', 'five', 'six'] 70 | ``` 71 | 72 | ### thisArg 73 | 74 | Invocation context may be passed as the last argument. 75 | 76 | ```js 77 | var array = ['a', 'b', 'c']; 78 | var ctx = {a: 'aaa', b: 'bbb', c: 'ccc'}; 79 | 80 | var res = map(array, function(item, index, array) { 81 | return this[item]; 82 | }, ctx); 83 | 84 | console.log(res); 85 | // => ['aaa', 'bbb', 'ccc'] 86 | ``` 87 | 88 | ## About 89 | 90 | ### Related projects 91 | 92 | * [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. This is the fastest implementation of array flatten. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays. This is the fastest implementation of array flatten.") 93 | * [arr-map](https://www.npmjs.com/package/arr-map): Faster, node.js focused alternative to JavaScript's native array map. | [homepage](https://github.com/jonschlinkert/arr-map "Faster, node.js focused alternative to JavaScript's native array map.") 94 | * [for-in](https://www.npmjs.com/package/for-in): Iterate over the own and inherited enumerable properties of an object, and return an object… [more](https://github.com/jonschlinkert/for-in) | [homepage](https://github.com/jonschlinkert/for-in "Iterate over the own and inherited enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js") 95 | * [for-own](https://www.npmjs.com/package/for-own): Iterate over the own enumerable properties of an object, and return an object with properties… [more](https://github.com/jonschlinkert/for-own) | [homepage](https://github.com/jonschlinkert/for-own "Iterate over the own enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning `false`. JavaScript/Node.js.") 96 | 97 | ### Contributing 98 | 99 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 100 | 101 | ### Building docs 102 | 103 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 104 | 105 | To generate the readme, run the following command: 106 | 107 | ```sh 108 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 109 | ``` 110 | 111 | ### Running tests 112 | 113 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 114 | 115 | ```sh 116 | $ npm install && npm test 117 | ``` 118 | 119 | ### Author 120 | 121 | **Jon Schlinkert** 122 | 123 | * [github/jonschlinkert](https://github.com/jonschlinkert) 124 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 125 | 126 | ### License 127 | 128 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 129 | Released under the [MIT License](LICENSE). 130 | 131 | *** 132 | 133 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.3, on March 02, 2017._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * collection-map 3 | * 4 | * Copyright (c) 2015, 2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var makeIterator = require('make-iterator'); 11 | var forOwn = require('for-own'); 12 | var map = require('arr-map'); 13 | 14 | module.exports = function(collection, fn, thisArg) { 15 | if (!Array.isArray(collection)) { 16 | var iterator = makeIterator(fn, thisArg); 17 | var result = []; 18 | 19 | forOwn(collection, function(value, key) { 20 | result.push(iterator(value, key, collection)); 21 | }); 22 | return result; 23 | } 24 | return map(collection, fn, thisArg); 25 | }; 26 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "collection-map", 3 | "description": "Returns an array of mapped values from an array or object.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/jonschlinkert/collection-map", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/collection-map", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/collection-map/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "arr-map": "^2.0.2", 24 | "for-own": "^1.0.0", 25 | "make-iterator": "^1.0.0" 26 | }, 27 | "devDependencies": { 28 | "gulp-format-md": "^0.1.11", 29 | "mocha": "^3.2.0" 30 | }, 31 | "keywords": [ 32 | "collection", 33 | "map" 34 | ], 35 | "verb": { 36 | "toc": false, 37 | "layout": "default", 38 | "tasks": [ 39 | "readme" 40 | ], 41 | "plugins": [ 42 | "gulp-format-md" 43 | ], 44 | "related": { 45 | "list": [ 46 | "arr-flatten", 47 | "arr-map", 48 | "for-in", 49 | "for-own" 50 | ] 51 | }, 52 | "lint": { 53 | "reflinks": true 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * collection-map 3 | * 4 | * Copyright (c) 2015, 2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | var assert = require('assert'); 12 | var map = require('./'); 13 | 14 | // => ['one', 'two', 'three'] 15 | describe('objects:', function() { 16 | var obj = {a: 'foo', b: 'bar', c: 'baz'}; 17 | 18 | it('should map values:', function() { 19 | var res = map(obj, function(val, key, obj) { 20 | return val; 21 | }); 22 | assert.deepEqual(res, ['foo', 'bar', 'baz']); 23 | }); 24 | 25 | it('should expose key', function() { 26 | var res = map(obj, function(val, key, obj) { 27 | return key; 28 | }); 29 | assert.deepEqual(res, ['a', 'b', 'c']); 30 | }); 31 | 32 | it('should support a string as the second argument', function() { 33 | var obj = { 34 | a: {aaa: 'one', bbb: 'four', ccc: 'seven'}, 35 | b: {aaa: 'two', bbb: 'five', ccc: 'eight'}, 36 | c: {aaa: 'three', bbb: 'six', ccc: 'nine'} 37 | }; 38 | assert.deepEqual(map(obj, 'aaa'), ['one', 'two', 'three']); 39 | }); 40 | }); 41 | 42 | describe('arrays:', function() { 43 | var arr = ['foo', 'bar', 'baz']; 44 | 45 | it('should map values:', function() { 46 | var res = map(arr, function(item, i, arr) { 47 | return item; 48 | }); 49 | assert.deepEqual(res, ['foo', 'bar', 'baz']); 50 | }); 51 | 52 | it('should map indices:', function() { 53 | var res = map(arr, function(item, i, arr) { 54 | return i; 55 | }); 56 | assert.deepEqual(res, [0, 1, 2]); 57 | }); 58 | }); 59 | 60 | describe('this arg:', function() { 61 | var arr = ['a', 'b', 'c']; 62 | var ctx = {a: 'aaa', b: 'bbb', c: 'ccc'}; 63 | 64 | it('should expose the third argument as `this`:', function() { 65 | var res = map(arr, function(item, i, arr) { 66 | return this[item]; 67 | }, ctx); 68 | assert.deepEqual(res, ['aaa', 'bbb', 'ccc']); 69 | }); 70 | }); 71 | 72 | describe('strings:', function() { 73 | it('should work with strings:', function() { 74 | var obj = { 75 | a: { 76 | aaa: 'one', 77 | bbb: 'four', 78 | ccc: 'seven' 79 | }, 80 | b: { 81 | aaa: 'two', 82 | bbb: 'five', 83 | ccc: 'eight' 84 | }, 85 | c: { 86 | aaa: 'three', 87 | bbb: 'six', 88 | ccc: 'nine' 89 | } 90 | }; 91 | var arr = [obj.a, obj.b, obj.c]; 92 | assert.deepEqual(map(obj, 'aaa'), ['one', 'two', 'three']); 93 | assert.deepEqual(map(arr, 'bbb'), ['four', 'five', 'six']); 94 | }); 95 | }); 96 | --------------------------------------------------------------------------------