├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Florian Morel 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | infinite-random-list 3 | === 4 | 5 | ### An infinite list of shuffled elements ∞ 6 | 7 | Takes a reference set of items , returns a random item from this array, infinitely. Never pass 2 identical items in a row. 8 | 9 | - non-deterministic 10 | - goes through a full set before shuffling again 11 | - will `slice()` the given array 12 | 13 | Useful for generating an infinite grid based on different patterns/positions, or showing random informations without showing the same item multiple times. 14 | 15 | ### Installation :package: 16 | 17 | `npm i infinite-random-list -S` 18 | 19 | ### Usage & example :floppy_disk: 20 | 21 | ``` 22 | import InfiniteRandomList from 'infinite-random-list' 23 | 24 | let list = new InfiniteRandomList(['Emma', 'Hannah', 'Alex', 'Mia']) 25 | list.get() // 'Alex' 26 | list.get() // 'Mia' 27 | list.get() // 'Emma' 28 | list.get() // 'Hannah' 29 | // shuffles again here 30 | list.get() // 'Mia' 31 | list.get() // 'Alex' 32 | ... 33 | ``` 34 | 35 | * `new InfiniteRandomList(items)` 36 | Returns a list instance populated with `items`. 37 | 38 | * `list.set(items)` 39 | Sets **items** (Array) as the reference items. 40 | 41 | * `list.get()` 42 | Returns one item from the original set (passed using `list.set(items)` or the constructor). Always returns something (i.e. the list doesn't get empty). Always return a different item. 43 | 44 | ### License :pencil: 45 | MIT. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var shuffle = require('array-shuffle'); 4 | 5 | module.exports = InfiniteRandomList; 6 | function InfiniteRandomList(items) { 7 | this.set(items); 8 | } 9 | 10 | InfiniteRandomList.prototype.set = function(items) { 11 | this.original = items.slice(); 12 | this.items = []; 13 | this.lastItem = null; 14 | }; 15 | 16 | InfiniteRandomList.prototype.get = function() { 17 | var item = this.items.pop(); 18 | 19 | if (item == undefined) { 20 | this.items = shuffle(this.original.slice()); 21 | var l = this.items.length - 1; 22 | // If first shuffled item is the same as the last one, push it further into the pile 23 | if (this.original.length > 1 && this.items[l] == this.lastItem) { 24 | var offset = Math.max(0, Math.floor(this.original.length * 0.5)); 25 | var swapIndex = Math.floor(Math.random() * (this.original.length - offset)); 26 | var temp = this.items[swapIndex]; 27 | this.items[swapIndex] = this.items[l]; 28 | this.items[l] = temp; 29 | } 30 | 31 | return this.get(); 32 | 33 | } else { 34 | // Store last item to compare to the newly shuffled array 35 | this.lastItem = item; 36 | } 37 | 38 | return item; 39 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "infinite-random-list", 3 | "version": "1.0.2", 4 | "description": "An infinite list of shuffled elements", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "browserify test/index.js | tap-closer | smokestack | faucet", 11 | "test-debug": "budo test/index.js --live" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/ayamflow/infinite-random-list.git" 16 | }, 17 | "keywords": [ 18 | "infinite", 19 | "random", 20 | "shuffle", 21 | "list", 22 | "array" 23 | ], 24 | "author": "Florian Morel", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/ayamflow/infinite-random-list/issues" 28 | }, 29 | "homepage": "https://github.com/ayamflow/infinite-random-list#readme", 30 | "dependencies": { 31 | "array-shuffle": "^1.0.1" 32 | }, 33 | "devDependencies": { 34 | "browserify": "^14.4.0", 35 | "budo": "^10.0.3", 36 | "faucet": "0.0.1", 37 | "smokestack": "^3.4.1", 38 | "tap-closer": "^1.0.0", 39 | "underscore": "^1.8.3" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _ = require('underscore'); 4 | var test = require('tape'); 5 | var InfiniteRandomList = require('../'); 6 | 7 | var set = [1, 2, 3, 4, 5]; 8 | 9 | // Each test is ran 100 times 10 | var repeater = _.times(100, function(){return 0;}); 11 | 12 | // test list contain the same items 13 | test('input/output comparison', function(assert) { 14 | var list = new InfiniteRandomList(set); 15 | var pass = repeater.reduce(function(reduce, item) { 16 | var output = _.times(set.length, function() { 17 | return list.get(); 18 | }); 19 | return set.length === _.union(output, set).length && reduce; 20 | }, true); 21 | 22 | assert.ok(pass, 'The list should return the same items as the reference set'); 23 | assert.end(); 24 | }); 25 | 26 | // test list is shuffled 27 | test('shuffle test', function(assert) { 28 | // Assume if at least 1 item is out of place, the array is considered shuffled 29 | var list = new InfiniteRandomList(set); 30 | var pass = repeater.reduce(function(reduce, item) { 31 | var output = _.times(set.length, function() { 32 | return list.get(); 33 | }); 34 | return output.reduce(function(reduce, value, i) { 35 | return reduce || value != set[i] 36 | }, reduce); 37 | }, true); 38 | 39 | assert.ok(pass, 'The list should return shuffled items'); 40 | assert.end(); 41 | }); 42 | 43 | // test 1st item and last are different 44 | test('different items test', function(assert) { 45 | var list = new InfiniteRandomList(set); 46 | 47 | var pass = repeater.reduce(function(reduce, item, i) { 48 | var lastItem; 49 | var firstItem; 50 | 51 | let length = i > 0 ? set.length - 2 : set.length - 1; 52 | _.times(length, function() { 53 | list.get(); 54 | }); 55 | 56 | lastItem = list.get(); 57 | // the list will reshuffle here 58 | firstItem = list.get(); 59 | 60 | return lastItem !== firstItem && reduce; 61 | }, true); 62 | 63 | assert.ok(pass, 'Two consecutive items should always be different'); 64 | assert.end(); 65 | }); --------------------------------------------------------------------------------