Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array-shuffle-fp

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-shuffle-fp - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

101

package.json
{
"name": "array-shuffle-fp",
"version": "0.1.0",
"description": "Randomize the order of items in an array using FP style",
"author": "gziolo",
"license": "GPL-2.0+",
"repository": {
"type": "git",
"url": "git@github.com:gziolo/array-shuffle-fp.git"
},
"engines": {
"node": ">=6.0.0"
},
"keywords": [
"array",
"fp",
"random",
"shuffle"
],
"main": "./src/index.js",
"scripts": {
"pretest": "eslint --max-warnings 0 src",
"test": "jest",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch"
},
"devDependencies": {
"eslint": "3.12.2",
"eslint-config-wpcalypso": "0.6.0",
"eslint-plugin-wpcalypso": "3.0.2",
"jest-cli": "18.1.0"
},
"eslintConfig": {
"env": {
"jasmine": true,
"jest": true,
"node": true
},
"extends": "wpcalypso",
"rules": {
"max-len": [ 2, { "code": 150 } ]
}
},
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"verbose": true
}
"name": "array-shuffle-fp",
"version": "0.2.0",
"description": "Randomize the order of items in an array using FP style",
"author": "gziolo",
"license": "GPL-2.0+",
"repository": {
"type": "git",
"url": "git@github.com:gziolo/array-shuffle-fp.git"
},
"engines": {
"node": ">=6.0.0"
},
"keywords": [
"array",
"fp",
"random",
"shuffle"
],
"main": "./src/index.js",
"scripts": {
"pretest": "eslint --max-warnings 0 src",
"test": "jest",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch"
},
"devDependencies": {
"eslint": "3.12.2",
"eslint-config-wpcalypso": "0.6.0",
"eslint-plugin-wpcalypso": "3.0.2",
"jest-cli": "18.1.0"
},
"eslintConfig": {
"env": {
"jasmine": true,
"jest": true,
"node": true
},
"extends": "wpcalypso",
"rules": {
"max-len": [
2,
{
"code": 150
}
]
}
},
"jest": {
"collectCoverageFrom": [
"src/**/*.js"
],
"verbose": true
}
}

@@ -1,5 +0,5 @@

# array-shuffle-fp [![Build Status](https://travis-ci.org/gziolo/array-shuffle-fp.svg?branch=master)](https://travis-ci.org/gziolo/array-shuffle-fp)
# array-shuffle-fp [![Build Status](https://travis-ci.org/gziolo/array-shuffle-fp.svg?branch=master)](https://travis-ci.org/gziolo/array-shuffle-fp) [![npm version](https://badge.fury.io/js/array-shuffle-fp.svg)](https://badge.fury.io/js/array-shuffle-fp)
> Randomize the order of items in an array using FP style.
Uses the modern version of th [Fisher–Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm):
Uses the modern version of the [Fisher–Yates algorithm](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm):

@@ -6,0 +6,0 @@ ```{r, eval = FALSE}

@@ -6,5 +6,6 @@ const {

length,
lessThan,
map,
nth,
updateMatchingValue
update
} = require( '../fp' );

@@ -49,2 +50,22 @@

describe( '#lessThan', () => {
it( 'returns false when value greater than limit provided', () => {
const result = lessThan( 5 )( 33 );
expect( result ).toBe( false );
} );
it( 'returns false when value equal to limit provided', () => {
const result = lessThan( 5 )( 5 );
expect( result ).toBe( false );
} );
it( 'returns thrue when value lower than limit provided', () => {
const result = lessThan( 5 )( 3 );
expect( result ).toBe( true );
} );
} );
describe( '#map', () => {

@@ -78,15 +99,9 @@ it( 'returns the list with x appended to each element of the the given list', () => {

describe( '#updateValue', () => {
it( 'returns the same value when the value to compare differs', () => {
const result = updateMatchingValue( 'a', 'b' )( 'c' );
describe( '#update', () => {
it( 'returns a new copy of the list with the element at the provided index replaced with the given value', () => {
const result = update( 1 )( 'val' )( input );
expect( result ).toBe( 'c' );
expect( result ).toEqual( [ 'a', 'val', 'c', 'd', 'e' ] );
} );
it( 'returns replacement value when the value to compare matches', () => {
const result = updateMatchingValue( 'a', 'b' )( 'a' );
expect( result ).toBe( 'b' );
} );
} );
} );

@@ -24,10 +24,23 @@ const arrayShuffle = require( '../' );

it( 'returns a different array when more than one item provided', () => {
const input = Object.freeze( [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ] );
it( 'returns a different array containing the same items when unique items provided', () => {
const input = Object.freeze( [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' ] );
const result = arrayShuffle( input );
expect( result ).toHaveLength( 8 );
expect( result ).toHaveLength( 10 );
expect( result ).toEqual( expect.arrayContaining( input ) );
expect( result ).not.toEqual( input );
} );
it( 'returns a different array containing the same items when duplicated items provided', () => {
const equals = a => b => a === b;
const input = Object.freeze( [ 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b' ] );
const result = arrayShuffle( input );
expect( result ).toHaveLength( 10 );
expect( result.filter( equals( 'a' ) ) ).toHaveLength( 5 );
expect( result.filter( equals( 'b' ) ) ).toHaveLength( 5 );
expect( result ).not.toEqual( input );
} );
} );

@@ -9,2 +9,4 @@ const compose = ( f, g ) => val => f( g( val ) );

const lessThan = limit => val => val < limit;
const map = fn => list => list.map( fn );

@@ -14,4 +16,10 @@

const updateMatchingValue = ( from, to ) => val => val === from ? to : val;
const update = idx => x => list => {
const res = [ ...list ];
res[ idx ] = x;
return res;
};
module.exports = {

@@ -22,5 +30,6 @@ compose,

length,
lessThan,
map,
nth,
updateMatchingValue
update
};

@@ -6,18 +6,21 @@ const {

length,
map,
updateMatchingValue
lessThan,
nth,
update
} = require( './fp' );
const { pickRandom } = require( './rand' );
const { pickRandomIndex } = require( './rand' );
const lessThan2Elements = compose( lessThan( 2 ), length );
function arrayShuffle( scratch, result = [] ) {
if ( length( scratch ) < 2 ) {
if ( lessThan2Elements( scratch ) ) {
return [ ...scratch, ...result ];
}
const pick = pickRandom( scratch );
const updateMatchingPickWithLast = updateMatchingValue( pick, last( scratch ) );
const pickIndex = pickRandomIndex( scratch );
const updatePickWithLast = update( pickIndex )( last( scratch ) );
return arrayShuffle(
compose( map( updateMatchingPickWithLast ), init )( scratch ),
[ pick, ...result ]
compose( init, updatePickWithLast )( scratch ),
[ nth( pickIndex )( scratch ), ...result ]
);

@@ -31,3 +34,3 @@ }

if ( length( input ) < 2 ) {
if ( lessThan2Elements( input ) ) {
return input;

@@ -34,0 +37,0 @@ }

@@ -1,11 +0,7 @@

const { nth } = require( './fp' );
function pickRandom( list ) {
const roll = Math.floor( Math.random() * list.length );
return nth( roll )( list );
function pickRandomIndex( list ) {
return Math.floor( Math.random() * list.length );
}
module.exports = {
pickRandom
pickRandomIndex
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc