Skip to content

Commit

Permalink
Adds Placeholder class for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
jaubourg committed Apr 2, 2017
1 parent 7814687 commit 7a667c2
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 26 deletions.
25 changes: 13 additions & 12 deletions lib/Args.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use strict";

const placeholder = Symbol( `placeholder` );
const Placeholder = require( `./Placeholder` );

class Args {
constructor( args, from = 0 ) {
if ( args ) {
this.args = args;
this.placeholders = [];
for ( let i = 0; i < args.length; i++ ) {
if ( args[ i ] === placeholder ) {
if ( args[ i ] instanceof Placeholder ) {
this.placeholders.push( i + from );
}
}
Expand Down Expand Up @@ -37,13 +37,12 @@ class Args {
let index = 0;
// try & fill as many placeholders as possible
for ( ; ( index < placeholders.length ) && ( index < args.length ); index++ ) {
const item = args[ index ];
const targetIndex = placeholders[ index ];
if ( item === placeholder ) {
const replaced = targetArgs[ targetIndex ].replaceWith( args[ index ] );
if ( replaced instanceof Placeholder ) {
targetPlaceholders.push( targetIndex );
} else {
targetArgs[ targetIndex ] = item;
}
targetArgs[ targetIndex ] = replaced;
}
// keep track of the placeholders who couldn't be filled
for ( let i = index; i < placeholders.length; i++ ) {
Expand All @@ -52,7 +51,7 @@ class Args {
// push the remaining args
for ( ; index < args.length; index++ ) {
const item = args[ index ];
if ( item === placeholder ) {
if ( item instanceof Placeholder ) {
targetPlaceholders.push( targetArgs.length );
}
targetArgs.push( item );
Expand All @@ -64,13 +63,15 @@ class Args {
return newInstance;
}
toArray() {
if ( this.placeholders.length ) {
throw new Error( `missing arguments` );
if ( !this.placeholders.length ) {
return this.args;
}
const args = this.args.slice();
for ( const index of this.placeholders ) {
args[ index ] = args[ index ].default();
}
return this.args;
return args;
}
}

Args.placeholder = placeholder;

module.exports = Args;
25 changes: 25 additions & 0 deletions lib/Placeholder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

const hasDefaultSymbol = Symbol( `hasDefault` );
const defaultSymbol = Symbol( `default` );

class Placeholder {
constructor( value ) {
this[ hasDefaultSymbol ] = arguments.length;
this[ defaultSymbol ] = value;
}
default() {
if ( !this[ hasDefaultSymbol ] ) {
throw new Error( `missing argument` );
}
return this[ defaultSymbol ];
}
replaceWith( value ) {
if ( value instanceof Placeholder ) {
return value[ hasDefaultSymbol ] ? value : this;
}
return value;
}
}

module.exports = Placeholder;
12 changes: 5 additions & 7 deletions lib/Prall.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ class Prall extends ExtensibleFunction {
return new Promise( ( resolve, reject ) => {
const context = this[ contextSymbol ].get();
const func = getFunction( context, this[ methodSymbol ] );
let argsArray = this[ argsSymbol ].concat( callArgs ).toArray();
const args = this[ argsSymbol ].concat( callArgs ).toArray();
const adapt = this[ adaptSymbol ];
if ( adapt ) {
argsArray = argsArray.concat( [ ( ...callbackArgs ) => {
func.call( context, ...args, ( ...callbackArgs ) => {
try {
const [ error, success ] = adapt( ...callbackArgs );
if ( error ) {
Expand All @@ -55,11 +55,9 @@ class Prall extends ExtensibleFunction {
} catch ( e ) {
reject( e );
}
} ] );
}
const returned = func.apply( context, argsArray );
if ( !adapt ) {
resolve( returned );
} );
} else {
resolve( func.apply( context, args ) );
}
} );
}
Expand Down
12 changes: 8 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ adapt.prall = prall;
prall.adapt = adapt;
prall.prall = prall;

const { placeholder } = require( `./Args` );
const Placeholder = require( `./Placeholder` );
const placeholder = new Placeholder();

prall.placeholder = placeholder;
prall._ = placeholder;
adapt.placeholder = placeholder;
adapt._ = placeholder;
adapt.placeholder = placeholder;
adapt.Placeholder = Placeholder;

prall._ = placeholder;
prall.placeholder = placeholder;
prall.Placeholder = Placeholder;

Object.freeze( adapt );
Object.freeze( prall );
Expand Down
24 changes: 21 additions & 3 deletions test/units/007_placeholder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

const { _ } = require( `../..` );
const { _, Placeholder } = require( `../..` );

module.exports = require( `../both` )( {
"1st of 3": ( prall, concat ) => assert => {
Expand Down Expand Up @@ -66,10 +66,10 @@ module.exports = require( `../both` )( {
)
.then( () => assert.done() );
},
"missing arguments": ( prall, concat ) => assert => {
"missing argument": ( prall, concat ) => assert => {
assert.expect( 1 );
prall( concat, `a` ).with( _, `c` )()
.catch( error => assert.strictEqual( error.message, `missing arguments` ) )
.catch( error => assert.strictEqual( error.message, `missing argument` ) )
.then( () => assert.done() );
},
"adding placeholders in placeholders": ( prall, concat ) => assert => {
Expand All @@ -83,6 +83,24 @@ module.exports = require( `../both` )( {
)
.then( () => assert.done() );
},
"placeholder with default": ( prall, concat ) => assert => {
assert.expect( 1 );
prall( concat, new Placeholder( `a` ), `b` )
.then(
string => assert.strictEqual( string, `ab` ),
() => null
)
.then( () => assert.done() );
},
"placeholder replace with default": ( prall, concat ) => assert => {
assert.expect( 1 );
prall( concat, _, `b` ).with( new Placeholder( `a` ) )
.then(
string => assert.strictEqual( string, `ab` ),
() => null
)
.then( () => assert.done() );
},
}, ( ...args ) => {
let result = ``;
for ( const string of args ) {
Expand Down

0 comments on commit 7a667c2

Please sign in to comment.