Browse Source
- Revert ES6 changes to arithmetics.pegjs - Use Array#forEach instead of for..of - Don't use native Array#find & Array#findIndex - Added util/arrays.js (find & findIndex) - Use Function instead of evalmaster
12 changed files with 89 additions and 78 deletions
@ -0,0 +1,33 @@ |
|||
"use strict"; |
|||
|
|||
/** |
|||
* The `findIndex()` method returns the index of the first element in the array that satisfies the |
|||
* provided testing function, otherwise `-1` is returned. |
|||
*/ |
|||
function findIndex( array, condition ) { |
|||
|
|||
for ( let i = 0; i < array.length; ++i ) { |
|||
|
|||
if ( condition( array[ i ], i ) ) return i; |
|||
|
|||
} |
|||
|
|||
return -1; |
|||
|
|||
} |
|||
|
|||
/** |
|||
* The `find()` method returns the value of the first element in the array that satisfies the |
|||
* provided testing function, otherwise `undefined` is returned. |
|||
*/ |
|||
function find( array, condition ) { |
|||
|
|||
const index = findIndex( array, condition ); |
|||
|
|||
return index < 0 ? void 0 : array[ index ]; |
|||
|
|||
} |
|||
|
|||
// Exports
|
|||
|
|||
module.exports = { findIndex, find }; |
@ -1,58 +1,26 @@ |
|||
"use strict"; |
|||
|
|||
const code = ( () => { |
|||
/** |
|||
* `eval` the given source, using properties found in `context` as top-level variables. |
|||
* |
|||
* Based on `vm.runInContext` found in Node.js, this is a cross-env solution. |
|||
*/ |
|||
function runInContext( source, context ) { |
|||
|
|||
let preface = ""; |
|||
const MODULE_VARS = { |
|||
const argumentKeys = Object.keys( context ); |
|||
const argumentValues = argumentKeys.map( argument => context[ argument ] ); |
|||
|
|||
"module": true, |
|||
"process": true, |
|||
"code": true, |
|||
"runInContext": true, |
|||
"source": true, |
|||
"preface": true, |
|||
const object = {}; |
|||
argumentKeys.push( "_peg$object", `_peg$object.result = ${ source };` ); |
|||
argumentValues.push( object ); |
|||
|
|||
}; |
|||
Function( ...argumentKeys )( ...argumentValues ); |
|||
|
|||
Object.keys( MODULE_VARS ).forEach( name => { |
|||
return object.result; |
|||
|
|||
preface += `var ${ name } = void 0;`; |
|||
|
|||
} ); |
|||
} |
|||
|
|||
function generate( name ) { |
|||
// Exports
|
|||
|
|||
return `${ ( MODULE_VARS[ name ] ? "" : "var " ) + name } = __context.${ name };`; |
|||
|
|||
} |
|||
|
|||
return { generate, preface }; |
|||
|
|||
} )(); |
|||
|
|||
module.exports = { |
|||
|
|||
// `eval` the given source, using properties found in `context` as top-level
|
|||
// variables, while hiding some variables in this module from the source.
|
|||
//
|
|||
// Based on `vm.runInContext` found in Node.js, this is a cross-env solution.
|
|||
runInContext( source, __context ) { |
|||
|
|||
let preface = code.preface; |
|||
|
|||
if ( typeof __context === "object" ) { |
|||
|
|||
Object.keys( __context ).forEach( name => { |
|||
|
|||
preface += code.generate( name ); |
|||
|
|||
} ); |
|||
|
|||
} |
|||
|
|||
return eval( preface + source ); |
|||
|
|||
|
|||
}, |
|||
|
|||
}; |
|||
module.exports = { runInContext }; |
|||
|
Loading…
Reference in new issue