├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── benchmark ├── check.js ├── code │ ├── array-from.js │ ├── array-map-call.js │ ├── array-map-valueOf.js │ ├── current.js │ ├── for-concat.js │ ├── for-join-1.js │ ├── for-join-2.js │ ├── for-length.js │ ├── for-new-array-1.js │ ├── for-new-array-2.js │ ├── for-push.js │ ├── new-array-map-1.js │ ├── new-array-map-2.js │ ├── repeat-element.js │ ├── while-array-1.js │ ├── while-array-2.js │ ├── while-array-3.js │ ├── while-bitwise.js │ ├── while-concat.js │ ├── while-exp.js │ ├── while-new-array-1.js │ ├── while-new-array-2.js │ ├── while-push.js │ ├── while-stack.js │ └── while-unshift.js ├── fixtures │ ├── 0.js │ ├── 1.js │ ├── 100.js │ ├── 2.js │ ├── 2000.js │ ├── 20000.js │ ├── 25.js │ ├── 250.js │ ├── 3.js │ ├── 4.js │ ├── 5.js │ └── 50.js ├── index.js └── last.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | *.* text eol=lf 3 | *.css text eol=lf 4 | *.html text eol=lf 5 | *.js text eol=lf 6 | *.json text eol=lf 7 | *.less text eol=lf 8 | *.md text eol=lf 9 | *.yml text eol=lf 10 | 11 | *.jpg binary 12 | *.gif binary 13 | *.png binary 14 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | .idea 4 | .vscode 5 | *.sublime-* 6 | 7 | # test related, or directories generated by tests 8 | test/actual 9 | actual 10 | coverage 11 | .nyc* 12 | 13 | # npm 14 | node_modules 15 | npm-debug.log 16 | 17 | # yarn 18 | yarn.lock 19 | yarn-error.log 20 | 21 | # misc 22 | _gh_pages 23 | _draft 24 | _drafts 25 | bower_components 26 | vendor 27 | temp 28 | tmp 29 | TODO.md 30 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'node' 5 | - '6' 6 | - '5' 7 | - '4' 8 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | const repeat = require('{%= name %}'); 5 | 6 | repeat('a', 5); 7 | //=> ['a', 'a', 'a', 'a', 'a'] 8 | 9 | repeat('a', 1); 10 | //=> ['a'] 11 | 12 | repeat('a', 0); 13 | //=> [] 14 | 15 | repeat(null, 5) 16 | //» [ null, null, null, null, null ] 17 | 18 | repeat({some: 'object'}, 5) 19 | //» [ { some: 'object' }, 20 | // { some: 'object' }, 21 | // { some: 'object' }, 22 | // { some: 'object' }, 23 | // { some: 'object' } ] 24 | 25 | repeat(5, 5) 26 | //» [ 5, 5, 5, 5, 5 ] 27 | ``` 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, 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 | # repeat-element [![NPM version](https://img.shields.io/npm/v/repeat-element.svg?style=flat)](https://www.npmjs.com/package/repeat-element) [![NPM monthly downloads](https://img.shields.io/npm/dm/repeat-element.svg?style=flat)](https://npmjs.org/package/repeat-element) [![NPM total downloads](https://img.shields.io/npm/dt/repeat-element.svg?style=flat)](https://npmjs.org/package/repeat-element) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/repeat-element.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/repeat-element) 2 | 3 | > Create an array by repeating the given value n times. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save repeat-element 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | const repeat = require('repeat-element'); 19 | 20 | repeat('a', 5); 21 | //=> ['a', 'a', 'a', 'a', 'a'] 22 | 23 | repeat('a', 1); 24 | //=> ['a'] 25 | 26 | repeat('a', 0); 27 | //=> [] 28 | 29 | repeat(null, 5) 30 | //» [ null, null, null, null, null ] 31 | 32 | repeat({some: 'object'}, 5) 33 | //» [ { some: 'object' }, 34 | // { some: 'object' }, 35 | // { some: 'object' }, 36 | // { some: 'object' }, 37 | // { some: 'object' } ] 38 | 39 | repeat(5, 5) 40 | //» [ 5, 5, 5, 5, 5 ] 41 | ``` 42 | 43 | ## About 44 | 45 |
46 | Contributing 47 | 48 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 49 | 50 |
51 | 52 |
53 | Running Tests 54 | 55 | 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: 56 | 57 | ```sh 58 | $ npm install && npm test 59 | ``` 60 | 61 |
62 | 63 |
64 | Building docs 65 | 66 | _(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.)_ 67 | 68 | To generate the readme, run the following command: 69 | 70 | ```sh 71 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 72 | ``` 73 | 74 |
75 | 76 | ### Contributors 77 | 78 | | **Commits** | **Contributor** | 79 | | --- | --- | 80 | | 17 | [jonschlinkert](https://github.com/jonschlinkert) | 81 | | 3 | [LinusU](https://github.com/LinusU) | 82 | | 1 | [architectcodes](https://github.com/architectcodes) | 83 | 84 | ### Author 85 | 86 | **Jon Schlinkert** 87 | 88 | * [GitHub Profile](https://github.com/jonschlinkert) 89 | * [Twitter Profile](https://twitter.com/jonschlinkert) 90 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 91 | 92 | ### License 93 | 94 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 95 | Released under the [MIT License](LICENSE). 96 | 97 | *** 98 | 99 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on August 19, 2018._ -------------------------------------------------------------------------------- /benchmark/check.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const chalk = require('chalk'); 4 | const path = require('path'); 5 | const glob = require('glob'); 6 | const repeat = require('../'); 7 | 8 | /** 9 | * Sanity check. Run to ensure that all fns return the same result. 10 | */ 11 | 12 | let fixtures = glob.sync(__dirname + '/fixtures/*.js').map(require); 13 | let expected = fixtures.map(fixture => repeat.apply(repeat, fixture).length); 14 | 15 | glob.sync(__dirname + '/code/*.js').forEach(function (fp) { 16 | let fn = require(path.resolve(__dirname, 'code', fp)); 17 | let name = path.basename(fp, path.extname(fp)); 18 | let problems = []; 19 | 20 | fixtures.forEach(function (fixture, idx) { 21 | let answer = fn.apply(fn, fixture).length; 22 | 23 | if (answer !== expected[idx]) { 24 | problems.push(['repeat(' + fixture.join(', ') + ').length', answer, expected[idx]]); 25 | } 26 | }); 27 | 28 | if (problems.length === 0) { 29 | console.log(' ' + chalk.bold.green('✔') + ' ' + chalk.bold(name)); 30 | } else { 31 | console.log(' ' + chalk.bold.red('✖') + ' ' + chalk.bold(name)); 32 | 33 | problems.forEach(function (item, idx, arr) { 34 | let str = item[0] + ' gave ' + item[1] + ', expected ' + item[2]; 35 | console.log((idx === arr.length - 1 ? ' ┗ ' : ' ┣ ') + chalk.red(str)); 36 | }); 37 | } 38 | }); 39 | -------------------------------------------------------------------------------- /benchmark/code/array-from.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function getThis() { 4 | return this; 5 | } 6 | 7 | module.exports = function repeat(ele, num) { 8 | return Array.from({length: num}, getThis, ele); 9 | }; 10 | -------------------------------------------------------------------------------- /benchmark/code/array-map-call.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | return Array.prototype.map.call([] + Array(num + 1), function () { 5 | return ele; 6 | }); 7 | }; 8 | -------------------------------------------------------------------------------- /benchmark/code/array-map-valueOf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | return Array.apply(null, Array(num)).map(String.prototype.valueOf, ele); 5 | }; 6 | -------------------------------------------------------------------------------- /benchmark/code/current.js: -------------------------------------------------------------------------------- 1 | module.exports = require('../..'); 2 | -------------------------------------------------------------------------------- /benchmark/code/for-concat.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var arr = []; 5 | 6 | for (var i = 0; i < num; i++) { 7 | arr = arr.concat(ele); 8 | } 9 | 10 | return arr; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/for-join-1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var arr = []; 5 | 6 | for (; 0 < num; num -= 1, arr[num] = ele); 7 | num++; 8 | 9 | return arr; 10 | }; 11 | -------------------------------------------------------------------------------- /benchmark/code/for-join-2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var arr = []; 5 | 6 | for (; 0 < num; arr[--num] = ele); 7 | 8 | return arr; 9 | }; 10 | -------------------------------------------------------------------------------- /benchmark/code/for-length.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = repeat; 4 | 5 | function repeat(ele, num) { 6 | if (num === 0) return []; 7 | 8 | var res = [ele]; 9 | var len = res.length; 10 | 11 | if (len < num) { 12 | for (var i = num - 1 - len; i >= 0; i--) { 13 | res[i + len] = res[i % len]; 14 | } 15 | } 16 | return res; 17 | } 18 | -------------------------------------------------------------------------------- /benchmark/code/for-new-array-1.js: -------------------------------------------------------------------------------- 1 | module.exports = repeat; 2 | 3 | function repeat(ele, num) { 4 | var arr = new Array(num); 5 | 6 | for (var i = 0; i < num; i++) { 7 | arr[i] = ele; 8 | } 9 | 10 | return arr; 11 | } 12 | -------------------------------------------------------------------------------- /benchmark/code/for-new-array-2.js: -------------------------------------------------------------------------------- 1 | module.exports = repeat; 2 | 3 | function repeat(ele, num) { 4 | var arr = new Array(num); 5 | 6 | for (var i = num - 1; i >= 0; i--) { 7 | arr[i] = ele; 8 | } 9 | 10 | return arr; 11 | } 12 | -------------------------------------------------------------------------------- /benchmark/code/for-push.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var arr = []; 5 | 6 | for (var i = num; i > 0; i--) { 7 | arr.push(ele); 8 | } 9 | 10 | return arr; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/new-array-map-1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | return Array.apply([], new Array(num)).map(function (nil) { 5 | return ele; 6 | }); 7 | }; 8 | -------------------------------------------------------------------------------- /benchmark/code/new-array-map-2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | return Array(num + 1).join(1).split('').map(function (nil) { 5 | return ele; 6 | }); 7 | }; 8 | 9 | -------------------------------------------------------------------------------- /benchmark/code/repeat-element.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = require('../..'); -------------------------------------------------------------------------------- /benchmark/code/while-array-1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(val, num) { 4 | var arr = []; 5 | 6 | while (num--) { 7 | arr[num] = val; 8 | } 9 | 10 | return arr; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/while-array-2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(val, num) { 4 | var arr = []; 5 | var i = 0; 6 | 7 | while (num--) { 8 | arr[i++] = val; 9 | } 10 | 11 | return arr; 12 | }; 13 | -------------------------------------------------------------------------------- /benchmark/code/while-array-3.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(val, num) { 4 | return repeat(val, num, []); 5 | }; 6 | 7 | function repeat(val, num, arr) { 8 | while (num--) { 9 | arr[num] = val; 10 | } 11 | return arr; 12 | } 13 | -------------------------------------------------------------------------------- /benchmark/code/while-bitwise.js: -------------------------------------------------------------------------------- 1 | 2 | module.exports = repeat; 3 | 4 | function repeat(ele, num) { 5 | var arr = [ele]; 6 | var res = []; 7 | 8 | while (num > 0) { 9 | if (num & 1) { 10 | res.push.apply(res, arr); 11 | } 12 | num >>= 1; 13 | arr.push.apply(arr, arr); 14 | } 15 | return res; 16 | } 17 | -------------------------------------------------------------------------------- /benchmark/code/while-concat.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var res = []; 5 | 6 | while (num--) { 7 | res = res.concat(ele); 8 | } 9 | 10 | return res; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/while-exp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var res = [ele]; 5 | 6 | while (res.length < num) { 7 | res = res.concat(res); 8 | } 9 | 10 | return res.slice(0, num); 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/while-new-array-1.js: -------------------------------------------------------------------------------- 1 | module.exports = repeat; 2 | 3 | function repeat(ele, num) { 4 | var arr = new Array(num); 5 | 6 | while (num--) { 7 | arr[num] = ele; 8 | } 9 | 10 | return arr; 11 | } 12 | -------------------------------------------------------------------------------- /benchmark/code/while-new-array-2.js: -------------------------------------------------------------------------------- 1 | module.exports = repeat; 2 | 3 | function repeat(ele, num) { 4 | var arr = new Array(num); 5 | var i = 0; 6 | 7 | while (num--) { 8 | arr[i++] = ele; 9 | } 10 | 11 | return arr; 12 | } 13 | -------------------------------------------------------------------------------- /benchmark/code/while-push.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var arr = []; 5 | 6 | while (num--) { 7 | arr.push(ele); 8 | } 9 | 10 | return arr; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/code/while-stack.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(ele, num) { 4 | var res = [ele]; 5 | var max = num; 6 | 7 | while (num--) { 8 | res.push.apply(res, res); 9 | if (res.length >= max) { 10 | break; 11 | } 12 | } 13 | 14 | return res.slice(0, max); 15 | }; 16 | -------------------------------------------------------------------------------- /benchmark/code/while-unshift.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function repeat(val, amount) { 4 | var res = []; 5 | 6 | while (amount--) { 7 | res.unshift(val); 8 | } 9 | 10 | return res; 11 | }; 12 | -------------------------------------------------------------------------------- /benchmark/fixtures/0.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['afa', 0]; -------------------------------------------------------------------------------- /benchmark/fixtures/1.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['abc', 1]; -------------------------------------------------------------------------------- /benchmark/fixtures/100.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['fas', 100]; 4 | 5 | -------------------------------------------------------------------------------- /benchmark/fixtures/2.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['def', 2]; -------------------------------------------------------------------------------- /benchmark/fixtures/2000.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['lll', 2000]; 4 | 5 | -------------------------------------------------------------------------------- /benchmark/fixtures/20000.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['lll', 20000]; 4 | 5 | -------------------------------------------------------------------------------- /benchmark/fixtures/25.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['iai', 25]; -------------------------------------------------------------------------------- /benchmark/fixtures/250.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['bbb', 250]; 4 | 5 | -------------------------------------------------------------------------------- /benchmark/fixtures/3.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['ghi', 3]; -------------------------------------------------------------------------------- /benchmark/fixtures/4.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['xyz', 4]; -------------------------------------------------------------------------------- /benchmark/fixtures/5.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['fff', 5]; -------------------------------------------------------------------------------- /benchmark/fixtures/50.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = ['xyz', 50]; 4 | 5 | -------------------------------------------------------------------------------- /benchmark/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Suite = require('benchmarked'); 4 | const suite = new Suite({ 5 | result: false, 6 | fixtures: 'fixtures/{3,5,25,2000}.js', 7 | code: 'code/{current,while-push-new-array}.js', 8 | cwd: __dirname 9 | }); 10 | 11 | suite.run(); 12 | -------------------------------------------------------------------------------- /benchmark/last.md: -------------------------------------------------------------------------------- 1 | #1: 2000.js 2 | current.js x 291,925 ops/sec ±0.66% (97 runs sampled) 3 | 4 | #2: 25.js 5 | current.js x 9,940,492 ops/sec ±0.72% (93 runs sampled) 6 | 7 | #3: 3.js 8 | current.js x 18,709,467 ops/sec ±0.97% (95 runs sampled) 9 | 10 | #4: 5.js 11 | current.js x 17,771,539 ops/sec ±0.77% (95 runs sampled) 12 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * repeat-element 3 | * 4 | * Copyright (c) 2015-present, Jon Schlinkert. 5 | * Licensed under the MIT license. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | module.exports = function repeat(ele, num) { 11 | if (Array.prototype.fill) { 12 | return new Array(num).fill(ele); 13 | } 14 | 15 | var arr = new Array(num); 16 | 17 | for (var i = 0; i < num; i++) { 18 | arr[i] = ele; 19 | } 20 | 21 | return arr; 22 | }; 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "repeat-element", 3 | "description": "Create an array by repeating the given value n times.", 4 | "version": "1.1.4", 5 | "homepage": "https://github.com/jonschlinkert/repeat-element", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/repeat-element", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/repeat-element/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 | "devDependencies": { 23 | "benchmarked": "^2.0.0", 24 | "chalk": "^2.4.1", 25 | "glob": "^7.1.2", 26 | "gulp-format-md": "^1.0.0", 27 | "minimist": "^1.2.0", 28 | "mocha": "^3.5.3" 29 | }, 30 | "keywords": [ 31 | "array", 32 | "element", 33 | "repeat", 34 | "string" 35 | ], 36 | "verb": { 37 | "toc": false, 38 | "layout": "default", 39 | "tasks": [ 40 | "readme" 41 | ], 42 | "plugins": [ 43 | "gulp-format-md" 44 | ], 45 | "lint": { 46 | "reflinks": true 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * repeat-element 3 | * 4 | * Copyright (c) 2014 Jon Schlinkert, contributors. 5 | * Licensed under the MIT License 6 | */ 7 | 8 | 'use strict'; 9 | 10 | require('mocha'); 11 | var fs = require('fs'); 12 | var path = require('path'); 13 | var assert = require('assert'); 14 | var argv = require('minimist')(process.argv.slice(2)); 15 | var files = fs.readdirSync('./benchmark/code'); 16 | var repeat = require('./'); 17 | 18 | var keys = Object.keys(argv); 19 | if (keys && keys[1]) { 20 | var lib = files.filter(function(fp) { 21 | return keys[1] === path.basename(fp, path.extname(fp)); 22 | }); 23 | repeat = require(path.resolve('./benchmark/code/' + lib[0])); 24 | } 25 | 26 | it('should repeat the given string:', function() { 27 | assert.deepEqual(repeat('a', 5), ['a', 'a', 'a', 'a', 'a']); 28 | assert.deepEqual(repeat('a', 50), ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a','a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']); 29 | assert.deepEqual(repeat('a', 1), ['a']); 30 | assert.deepEqual(repeat('a', 0), []); 31 | }); 32 | 33 | it('should repeat the given object:', function() { 34 | assert.deepEqual(repeat({a: 'b'}, 5), [{a: 'b'},{a: 'b'},{a: 'b'},{a: 'b'},{a: 'b'}]); 35 | }); 36 | 37 | it('should repeat null:', function() { 38 | assert.deepEqual(repeat(null, 5), [null, null, null, null, null]); 39 | }); 40 | --------------------------------------------------------------------------------