Skip to content

Commit 4cbfb73

Browse files
committed
update build and query functions, add prepublish script
1 parent 42adbac commit 4cbfb73

File tree

9 files changed

+2479
-0
lines changed

9 files changed

+2479
-0
lines changed

Diff for: .babelrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": [
4+
"transform-object-rest-spread"
5+
]
6+
}

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

Diff for: .npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

Diff for: index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as build from './lib/build'
2+
import * as query from './lib/query'
3+
4+
export {
5+
build,
6+
query,
7+
}

Diff for: lib/build.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Functions to build nodes
3+
*/
4+
5+
export function applyNode(op, args, loc, options = {}) {
6+
return {
7+
type: 'Apply',
8+
op: op,
9+
args: args,
10+
loc: loc || {
11+
start: args[0].loc.start,
12+
end: args[args.length - 1].loc.end,
13+
},
14+
...options,
15+
}
16+
}
17+
18+
export function identifierNode(name, start, end) {
19+
return {
20+
type: 'Identifier',
21+
name: name,
22+
loc: {start, end},
23+
// TODO: add subscript
24+
}
25+
}
26+
27+
export function numberNode(value, start, end) {
28+
return {
29+
type: 'Number',
30+
value: value,
31+
loc: {start, end},
32+
}
33+
}
34+
35+
export function parensNode(body, start, end) {
36+
return {
37+
type: 'Parentheses',
38+
loc: {start, end},
39+
body: body
40+
}
41+
}

Diff for: lib/query.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Functions to query properties of nodes
3+
*/
4+
5+
export const isIdentifier = node => node.type === 'Identifier'
6+
export const isApply = node => node.type === 'Apply'
7+
8+
export const isOperation = node => isApply(node) && !isNumber(node)
9+
export const isFunction = node => isApply(node) && isIdentifier(node.op)
10+
11+
// TODO: curry it?
12+
const _isOp = (op, node) => isApply(node) && node.op === op
13+
14+
export const isAdd = node => _isOp('add', node)
15+
export const isMul = node => _isOp('mul', node)
16+
export const isPow = node => _isOp('pow', node)
17+
export const isNeg = node => _isOp('neg', node)
18+
export const isPos = node => _isOp('pos', node)
19+
20+
export const isNumber = node => {
21+
if (node.type === 'Number') {
22+
return true
23+
} else if (isNeg(node)) {
24+
return isNumber(node.args[0])
25+
} else {
26+
return false
27+
}
28+
}
29+
30+
// check if it's a number before trying to get its value
31+
export const getValue = node => {
32+
if (node.type === 'Number') {
33+
return parseFloat(node.value)
34+
} else if (isNeg(node)) {
35+
return -getValue(node.args[0])
36+
} else if (isPos(node)) {
37+
return getValue(node.args[0])
38+
}
39+
}

Diff for: package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "math-nodes",
3+
"version": "0.0.1",
4+
"description": "Creation and identification utility functions for nodes in math-ast ASTs",
5+
"main": "dist/math-nodes.js",
6+
"scripts": {
7+
"prepublish": "webpack",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/semantic-math/math-nodes.git"
13+
},
14+
"author": "Kevin Barabash <[email protected]>",
15+
"license": "MIT",
16+
"bugs": {
17+
"url": "https://github.com/semantic-math/math-nodes/issues"
18+
},
19+
"homepage": "https://github.com/semantic-math/math-nodes#readme",
20+
"devDependencies": {
21+
"babel-core": "^6.24.1",
22+
"babel-loader": "^7.0.0",
23+
"babel-preset-es2015": "^6.24.1",
24+
"babel-plugin-transform-object-rest-spread": "^6.22.0",
25+
"webpack": "^2.4.1"
26+
}
27+
}

Diff for: webpack.config.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
entry: "./index.js",
5+
output: {
6+
path: path.join(__dirname, "dist"),
7+
filename: "math-nodes.js",
8+
libraryTarget: "commonjs2"
9+
},
10+
module: {
11+
loaders: [
12+
{
13+
test: /\.js$/,
14+
exclude: /node_modules/,
15+
loader: 'babel-loader',
16+
}
17+
]
18+
}
19+
}

0 commit comments

Comments
 (0)