├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - '0.12' 5 | - 'iojs' 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 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 | # random-iterate 2 | 3 | Iterate values in a list in random order 4 | 5 | ``` 6 | npm install random-iterate 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/random-iterate.svg?style=flat)](http://travis-ci.org/mafintosh/random-iterate) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var iterate = require('random-iterate') 15 | var ite = iterate([1,2,3,4,5,6,7,8,9]) 16 | 17 | console.log(ite()) // maybe 4 18 | console.log(ite()) // maybe 9 19 | ... 7 more time ... 20 | console.log(ite()) // returns null (end of list) 21 | ``` 22 | 23 | ## License 24 | 25 | MIT 26 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var iterate = function (list) { 2 | var offset = 0 3 | return function () { 4 | if (offset === list.length) return null 5 | 6 | var len = list.length - offset 7 | var i = (Math.random() * len) | 0 8 | var el = list[offset + i] 9 | 10 | var tmp = list[offset] 11 | list[offset] = el 12 | list[offset + i] = tmp 13 | offset++ 14 | 15 | return el 16 | } 17 | } 18 | 19 | module.exports = iterate 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-iterate", 3 | "version": "1.0.1", 4 | "description": "Iterate values in a list in random order", 5 | "main": "index.js", 6 | "devDependencies": { 7 | "standard": "^3.7.2", 8 | "tape": "^4.0.0" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/mafintosh/random-iterate.git" 13 | }, 14 | "scripts": { 15 | "test": "standard && tape test.js" 16 | }, 17 | "author": "Mathias Buus (@mafintosh)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/mafintosh/random-iterate/issues" 21 | }, 22 | "homepage": "https://github.com/mafintosh/random-iterate" 23 | } 24 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | var iterate = require('./') 3 | 4 | tape('iterates all', function (t) { 5 | var ite = iterate([1, 2, 3, 4, 5, 6, 7, 8, 9]) 6 | var found = {} 7 | 8 | for (var i = 0; i < 9; i++) { 9 | var j = ite() 10 | t.ok(!!j, 'not null') 11 | if (found[j]) t.ok(false, 'duplicate') 12 | found[j] = true 13 | } 14 | 15 | t.ok(!ite(), 'no more') 16 | t.end() 17 | }) 18 | --------------------------------------------------------------------------------