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.3.0 to 0.4.0

2

package.json
{
"name": "array-shuffle-fp",
"version": "0.3.0",
"version": "0.4.0",
"description": "Randomize the order of items in an array using FP style",

@@ -5,0 +5,0 @@ "author": "gziolo",

const {
compose,
concat,
flip,
ifElse,
init,

@@ -9,2 +12,3 @@ last,

nth,
prepend,
update

@@ -17,2 +21,5 @@ } = require( '../fp' );

const toUpperCase = val => val.toUpperCase();
const throwError = () => {
throw new Error();
};

@@ -27,2 +34,32 @@ describe( '#compose', () => {

describe( '#concat', () => {
it( 'returns the result of concatenating the given lists', () => {
const result = concat( input )( [ 'f' ] );
expect( result ).toEqual( [ 'a', 'b', 'c', 'd', 'e', 'f' ] );
} );
} );
describe( '#flip', () => {
it( 'returns a new function with the first two params order reversed', () => {
const result = flip( concat )( [ 'f' ] )( input );
expect( result ).toEqual( [ 'a', 'b', 'c', 'd', 'e', 'f' ] );
} );
} );
describe( '#ifElse', () => {
it( 'will process the onTrue function when the result of the condition predicate is true', () => {
const result = ifElse( list => length( list ) > 0, last, throwError )( input );
expect( result ).toBe( 'e' );
} );
it( 'will process the onFalse function when the result of the condition predicate is false', () => {
const result = ifElse( list => length( list ) > 100, throwError, last )( input );
expect( result ).toBe( 'e' );
} );
} );
describe( '#init', () => {

@@ -100,2 +137,10 @@ it( 'returns all but the last element of the given list', () => {

describe( '#prepend', () => {
it( 'returns a new list with the given element at the front, followed by the contents of the list', () => {
const result = prepend( 'z' )( input );
expect( result ).toEqual( [ 'z', 'a', 'b', 'c', 'd', 'e' ] );
} );
} );
describe( '#update', () => {

@@ -102,0 +147,0 @@ it( 'returns a new copy of the list with the element at the provided index replaced with the given value', () => {

@@ -1,3 +0,16 @@

const compose = ( f, g ) => val => f( g( val ) );
const compose = ( f, g ) =>
val => f( g( val ) );
const concat = firstList =>
secondList => [].concat( firstList, secondList );
const flip = fn =>
b =>
a => fn( a )( b );
const ifElse = ( condition, onTrue, onFalse ) =>
( list ) => condition( list )
? onTrue( list )
: onFalse( list );
const init = list => list.slice( 0, list.length - 1 );

@@ -9,28 +22,39 @@

const lessThan = limit => val => val < limit;
const lessThan = limit =>
val => val < limit;
const map = fn => list => list.map( fn );
const map = fn =>
list => list.map( fn );
const nth = n => list => list[ n ];
const nth = n =>
list => list[ n ];
const trampoline = fn => ( ...args ) => {
let result = fn( ...args );
const prepend = el => concat( [ el ] );
while ( typeof result === 'function' ) {
result = result();
}
const trampoline = fn =>
( ...args ) => {
let result = fn( ...args );
return result;
};
while ( typeof result === 'function' ) {
result = result();
}
const update = idx => x => list => {
const res = [ ...list ];
return result;
};
res[ idx ] = x;
const update = idx =>
x =>
list => {
const result = [ ...list ];
return res;
};
result[ idx ] = x;
return result;
};
module.exports = {
compose,
concat,
flip,
ifElse,
init,

@@ -42,4 +66,5 @@ last,

nth,
prepend,
trampoline,
update
};
const {
compose,
concat,
flip,
ifElse,
init,

@@ -8,2 +11,3 @@ last,

nth,
prepend,
trampoline,

@@ -16,16 +20,26 @@ update

function arrayShuffle( scratch, result = [] ) {
if ( lessThan2Elements( scratch ) ) {
return [ ...scratch, ...result ];
const arrayShuffleImpl = result => ifElse(
lessThan2Elements,
flip( concat )( result ),
( scratch ) => {
const pickIndex = pickRandomIndex( scratch );
const pick = nth( pickIndex )( scratch );
const updatePickWithLast = compose(
update( pickIndex ), last
)( scratch );
return () => arrayShuffleImpl(
prepend( pick )( result )
)(
compose(
init, updatePickWithLast
)( scratch )
);
}
);
const pickIndex = pickRandomIndex( scratch );
const updatePickWithLast = update( pickIndex )( last( scratch ) );
const arrayShuffle = trampoline(
arrayShuffleImpl( [] )
);
return () => arrayShuffle(
compose( init, updatePickWithLast )( scratch ),
[ nth( pickIndex )( scratch ), ...result ]
);
}
module.exports = function( input ) {

@@ -40,3 +54,3 @@ if ( ! Array.isArray( input ) ) {

return trampoline( arrayShuffle )( input );
return arrayShuffle( input );
};
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