Skip to content

Commit

Permalink
refactor: add support for specifying JavaScript return type
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Oct 4, 2024
1 parent 8be8688 commit d780577
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
> var r = {{alias:@stdlib/random/iter/{{BASE_PRNG}}}}( {{RAND_MIN}}, {{RAND_MAX}} );
> var it = {{alias}}( r );
> var v = it.next().value
<number>
<{{JAVASCRIPT_RETURN_TYPE}}>
> v = it.next().value
<number>
<{{JAVASCRIPT_RETURN_TYPE}}>

See Also
--------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ type Iterator<T> = Iter<T> | IterableIterator<T>;
* var iter = {{ALIAS}}( random( {{RAND_MIN}}, {{RAND_MAX}} ) );
*
* var v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* // ...
*/
declare function {{ALIAS}}( iterator: Iterator<number> ): Iterator<number>;
declare function {{ALIAS}}( iterator: Iterator<{{JAVASCRIPT_RETURN_TYPE}}> ): Iterator<{{JAVASCRIPT_RETURN_TYPE}}>;


// EXPORTS //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function next() {

// The function returns an iterator...
{
{{ALIAS}}( iterator() ); // $ExpectType TypedIterator<number>
{{ALIAS}}( iterator() ); // $ExpectType TypedIterator<{{JAVASCRIPT_RETURN_TYPE}}>
}

// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
* var iter = {{ALIAS}}( random( {{RAND_MIN}}, {{RAND_MAX}} ) );
*
* var v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* // ...
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ var {{UNARY_ALIAS}} = require( '@{{UNARY_PKG}}' );
* var iter = {{ALIAS}}( random( {{RAND_MIN}}, {{RAND_MAX}} ) );
*
* var v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* v = iter.next().value;
* // returns <number>
* // returns <{{JAVASCRIPT_RETURN_TYPE}}>
*
* // ...
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ tape( 'the function returns an iterator protocol-compliant object', function tes

for ( i = 0; i < 100; i++ ) {
r = it.next();
t.equal( typeof r.value, 'number', 'returns a number' );
t.equal( typeof r.done, 'boolean', 'returns a boolean' );
t.equal( typeof r.value, '{{JAVASCRIPT_RETURN_TYPE}}', 'returns expected value' );
t.equal( typeof r.done, 'boolean', 'returns expected value' );
}
t.end();
});
Expand Down Expand Up @@ -110,7 +110,7 @@ tape( 'the function returns an iterator protocol-compliant object which {{TEST_D
r = it.next();
expected = {{UNARY_ALIAS}}( x.next().value );
t.equal( r.value, expected, 'returns expected value' );
t.equal( typeof r.done, 'boolean', 'returns a boolean' );
t.equal( typeof r.done, 'boolean', 'returns expected value' );
}
r = it.next();
t.equal( r.value, void 0, 'returns expected value' );
Expand Down Expand Up @@ -181,11 +181,11 @@ tape( 'the returned iterator has a `return` method for closing an iterator (no a
it = {{ALIAS}}( random( {{RAND_MIN}}, {{RAND_MAX}} ) );

r = it.next();
t.equal( typeof r.value, 'number', 'returns expected value' );
t.equal( typeof r.value, '{{JAVASCRIPT_RETURN_TYPE}}', 'returns expected value' );
t.equal( r.done, false, 'returns expected value' );

r = it.next();
t.equal( typeof r.value, 'number', 'returns expected value' );
t.equal( typeof r.value, '{{JAVASCRIPT_RETURN_TYPE}}', 'returns expected value' );
t.equal( r.done, false, 'returns expected value' );

r = it.return();
Expand All @@ -206,11 +206,11 @@ tape( 'the returned iterator has a `return` method for closing an iterator (argu
it = {{ALIAS}}( random( {{RAND_MIN}}, {{RAND_MAX}} ) );

r = it.next();
t.equal( typeof r.value, 'number', 'returns expected value' );
t.equal( typeof r.value, '{{JAVASCRIPT_RETURN_TYPE}}', 'returns expected value' );
t.equal( r.done, false, 'returns expected value' );

r = it.next();
t.equal( typeof r.value, 'number', 'returns expected value' );
t.equal( typeof r.value, '{{JAVASCRIPT_RETURN_TYPE}}', 'returns expected value' );
t.equal( r.done, false, 'returns expected value' );

r = it.return( 'finished' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ function prngEnvVars( obj ) {
return 'BASE_PRNG=\'' + p[ p.length-1 ] + '\',RAND_MIN=' + obj.parameters[ 0 ] +',RAND_MAX=' + obj.parameters[ 1 ];
}

/**
* Returns a `JAVASCRIPT_RETURN_TYPE` environment variable string.
*
* @private
* @param {string} dt - return type
* @returns {string} environment variable string
*
* @example
* var s = returnTypeEnvVar( 'number' );
* // returns 'JAVASCRIPT_RETURN_TYPE=\'number\''
*/
function returnTypeEnvVar( dt ) {
return 'JAVASCRIPT_RETURN_TYPE=\'' + dt + '\'';
}


// MAIN //

Expand Down Expand Up @@ -209,6 +224,10 @@ function main() {
envs.push( prngEnvVars( v[ 0 ].parameters.rand ) );
continue;
}
if ( k === 'returns' ) {
envs.push( returnTypeEnvVar( v.type.javascript ) );
continue;
}
}
cmd = envs.join( ' ' ) + ' . ' + SCAFFOLD_SCRIPT;
shell( cmd );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@
#
# Environment Variables:
#
# ALIAS Main export alias.
# DESC Default description.
# PKG_DESC Package description.
# MODULE_DESC Module description.
# MAIN_DESC Main export description.
# TEST_DESC Test description.
# DESC_LINK_TEXT README link text.
# KEYWORDS List of keywords.
# BASE_PRNG Base name of the package for generating pseudorandom numbers.
# RAND_MIN Minimum value for generated values.
# RAND_MAX Maximum value for generated values.
# VALUES_LEN_2 List of input values.
# ALIAS Main export alias.
# DESC Default description.
# PKG_DESC Package description.
# MODULE_DESC Module description.
# MAIN_DESC Main export description.
# TEST_DESC Test description.
# DESC_LINK_TEXT README link text.
# KEYWORDS List of keywords.
# BASE_PRNG Base name of the package for generating pseudorandom numbers.
# RAND_MIN Minimum value for generated values.
# RAND_MAX Maximum value for generated values.
# VALUES_LEN_2 List of input values.
# JAVASCRIPT_RETURN_TYPE JavaScript return value data type.
#

## USER-DEFINED VARIABLES ##
Expand Down Expand Up @@ -74,6 +75,9 @@ else
IFS=','; read -ra keywords <<< "${KEYWORDS}"; IFS=' ';
fi

# Define the JavaScript return data type:
javascript_return_type=${JAVASCRIPT_RETURN_TYPE:-'number'}

# Define a pseudorandom number generator for generating random values:
base_prng=${BASE_PRNG:-'uniform'}

Expand Down Expand Up @@ -190,11 +194,13 @@ files=(
)

# Create the destination directories...
# shellcheck disable=SC2048
for dir in ${dirs[*]}; do
mkdir -p "${dest_dir}/${dir}"
done

# Copy the scaffold files to the destination directory...
# shellcheck disable=SC2048
for file in ${files[*]}; do
cp "${this_dir}/data/${file//\./__}.txt" "${dest_dir}/${file}"
done
Expand Down Expand Up @@ -277,12 +283,15 @@ find_and_replace "${regex}"
regex="s/\\{\\{BASE_PRNG\\}\\}/${base_prng}/g;"
find_and_replace "${regex}"

regex="s/\\{\\{RAND_MIN\\}\\}/${base_prng}/g;"
regex="s/\\{\\{RAND_MIN\\}\\}/${rand_min}/g;"
find_and_replace "${regex}"

regex="s/\\{\\{RAND_MAX\\}\\}/${rand_max}/g;"
find_and_replace "${regex}"

regex="s/\\{\\{JAVASCRIPT_RETURN_TYPE\\}\\}/${javascript_return_type}/g;"
find_and_replace "${regex}"

keywords_sep='",\n "'
if [ "${#keywords[*]}" -eq 0 ]; then
words=''
Expand Down

0 comments on commit d780577

Please sign in to comment.