-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,29 +4,30 @@ | |
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/keyanzhang/trivial-js-interpreter.git" | ||
}, | ||
"keywords": [], | ||
"author": "Keyan Zhang <[email protected]> (http:https://keya.nz)", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/keyanzhang/trivial-js-interpreter/issues" | ||
}, | ||
"homepage": "https://github.com/keyanzhang/trivial-js-interpreter#readme", | ||
"devDependencies": { | ||
"babel-core": "^6.7.6", | ||
"babel-eslint": "^6.0.2", | ||
"babel-jest": "^11.0.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.7.0", | ||
"babel-polyfill": "^6.7.4", | ||
"babel-preset-es2015": "^6.6.0", | ||
"babel-preset-stage-0": "^6.5.0", | ||
"eslint": "^2.7.0", | ||
"eslint-config-airbnb": "^7.0.0", | ||
"eslint-plugin-babel": "^3.2.0", | ||
"expect": "^1.16.0", | ||
"jest": "^0.1.40", | ||
"rimraf": "^2.5.2" | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import fs from 'fs'; | ||
/* | ||
* Sorry if this guy makes you disappointed. It's just a wrapper around `fs.readFileSync`. | ||
*/ | ||
import { readFileSync } from 'fs'; | ||
|
||
const readFile = (filename: string) => fs.readFileSync(filename, 'utf8'); | ||
const readFile = (filename: string) => readFileSync(filename, 'utf8'); | ||
|
||
export { readFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
jest.unmock('../Environment').unmock('immutable'); | ||
|
||
const { | ||
emptyEnv, | ||
lookupEnv, | ||
extendEnv, | ||
batchExtendEnv, | ||
} = require('../Environment'); | ||
|
||
describe('Environment', () => { | ||
it('has an empty env that contains nothing', () => { | ||
expect(emptyEnv.size).toBe(0); | ||
}); | ||
|
||
it('should extend and lookup stuff correctly', () => { | ||
const env = extendEnv('foo', 999, emptyEnv); | ||
expect(lookupEnv('foo', env)).toBe(999); | ||
}); | ||
|
||
it('should batch extend stuff correctly', () => { | ||
const env = extendEnv('foo', 999, emptyEnv); | ||
const keys = [ 'foo', 'bar', 'james', 'huang' ]; | ||
const vals = [ 1, 2, 3, 4 ]; | ||
const extendedEnv = batchExtendEnv(keys, vals, env); | ||
|
||
expect(extendedEnv.size).toBe(4); | ||
expect(lookupEnv('james', extendedEnv)).toBe(3); | ||
expect(lookupEnv('foo', extendedEnv)).toBe(1); | ||
}); | ||
|
||
it('should throw when the argument count is not correct', () => { | ||
const env = extendEnv('foo', 999, emptyEnv); | ||
const keys = [ 'foo', 'bar', 'james', 'huang', 'yeah' ]; | ||
const vals = [ 1, 2, 3 ]; | ||
expect(() => { batchExtendEnv(keys, vals, env); }).toThrow(); | ||
}); | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
const isObject = (x) => x && typeof x === 'object' && !Array.isArray(x); | ||
|
||
/* | ||
* fn acts as both a mapFn and a predicate | ||
*/ | ||
const mapFilterObject = (obj, fn) => { | ||
const keys = Object.keys(obj); | ||
const result = {}; | ||
|
||
keys.forEach((key) => { | ||
const val = obj[key]; | ||
const fnResult = fn(val, key); // `fn` return `false` for removal, or a tuple of `[ key, val ]` | ||
if (fnResult) { | ||
const [ newKey, newVal ] = fnResult; | ||
result[newKey] = newVal; | ||
} | ||
}); | ||
|
||
return result; | ||
}; | ||
|
||
export { | ||
isObject, | ||
mapFilterObject, | ||
}; |