chimpanzee
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -11,2 +11,3 @@ "use strict"; | ||
exports.captureIf = captureIf; | ||
exports.modify = modify; | ||
exports.captureWithSchema = captureWithSchema; | ||
@@ -27,3 +28,3 @@ exports.captureIfWithSchema = captureIfWithSchema; | ||
function capture(params) { | ||
function capture(params, options) { | ||
return captureIf(function (obj) { | ||
@@ -38,2 +39,10 @@ return typeof obj !== "undefined"; | ||
function modify(comparand, modifier, params) { | ||
return take(typeof comparand === "function" ? comparand : function (x) { | ||
return x === comparand; | ||
}, undefined, params, { modifier: typeof modifier === "function" ? modifier : function (x) { | ||
return modifier; | ||
} }); | ||
} | ||
function captureWithSchema(schema, params) { | ||
@@ -50,8 +59,8 @@ return captureIfWithSchema(function (obj) { | ||
function literal(what, params) { | ||
return captureIf(function (x) { | ||
return take(function (x) { | ||
return x === what; | ||
}, params); | ||
}, undefined, params, { skipMessage: `Expected value to be ${ what }.` }); | ||
} | ||
function take(predicate, schema, params) { | ||
function take(predicate, schema, params, options = {}) { | ||
params = typeof params === "string" ? { key: params } : params; | ||
@@ -61,4 +70,4 @@ | ||
return predicate(obj) ? typeof schema !== "undefined" ? (0, _utils.waitForSchema)(schema, obj, context, function (result) { | ||
return result instanceof _results.Match ? new _results.Match(_extends({}, obj, result.value)) : new _results.Skip("Capture failed in inner schema."); | ||
}) : new _results.Match(obj) : new _results.Skip("Predicate returned false."); | ||
return result instanceof _results.Match ? new _results.Match(_extends({}, obj, options.modifier ? options.modifier(result.value) : result.value)) : new _results.Skip("Capture failed in inner schema."); | ||
}) : new _results.Match(options.modifier ? options.modifier(obj) : obj) : new _results.Skip(options.skipMessage || "Predicate returned false."); | ||
} | ||
@@ -65,0 +74,0 @@ |
@@ -48,2 +48,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "modify", { | ||
enumerable: true, | ||
get: function get() { | ||
return _capture.modify; | ||
} | ||
}); | ||
Object.defineProperty(exports, "take", { | ||
@@ -50,0 +56,0 @@ enumerable: true, |
{ | ||
"name": "chimpanzee", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"author": "Jeswin<jeswinpk@agilehead.com>", | ||
@@ -5,0 +5,0 @@ "scripts": { |
128
README.md
chimpanzee | ||
========== | ||
Chimpanzee is a library for pattern matching tree, especially ASTs. | ||
This documentation covers the important features, but is not exhaustive. | ||
Chimpanzee is a library with which you can write a "Schema" for traversing trees structures and capturing values of certain nodes. | ||
This is especially useful for analyzing ASTs, which can be fairly large and complex. Schemas can be composed, allowing common structures to be abstracted out. | ||
This is still work in progress. The first non-beta version will come out along with the release of the parent project (Isotropy). | ||
Installation | ||
@@ -30,3 +32,15 @@ ------------ | ||
## Simple capturing: capture() | ||
### Result Types | ||
The result of a match is one of four types. | ||
- Match | ||
- value: object | ||
- Skip | ||
- message: string | ||
- Fault | ||
- message: string | ||
- Empty (is a Match, but has no value) | ||
### Simple capturing: capture() | ||
Allows you to capture the value of a node. | ||
``` | ||
@@ -44,3 +58,4 @@ const input = { | ||
## Named capturing: capture(alias) | ||
### Named capturing: capture(alias) | ||
Capture the value of a node and assign a name to it. | ||
``` | ||
@@ -58,3 +73,4 @@ const input = { | ||
## Mismatched Tree | ||
### Capture and modify: capture(predicate, modifier) | ||
Capture the value of a node and assign a name to it. | ||
``` | ||
@@ -66,2 +82,18 @@ const input = { | ||
const schema = traverse({ | ||
hello: modify( | ||
x => x === "world", | ||
x => `${x}!!!` | ||
) | ||
}) | ||
//result is Match { value: { prop1: "world!!!" } } | ||
``` | ||
### Mismatched Tree | ||
``` | ||
const input = { | ||
hello: "world" | ||
} | ||
const schema = traverse({ | ||
something: "else", | ||
@@ -74,3 +106,3 @@ hello: capture() | ||
## Capture with predicate: captureIf(predicate) | ||
### Capture with predicate: captureIf(predicate) | ||
``` | ||
@@ -88,3 +120,3 @@ const input = { | ||
## Capturing Types | ||
### Capturing Types | ||
``` | ||
@@ -108,3 +140,3 @@ const input = { | ||
## Capture a Literal | ||
### Capture a Literal | ||
``` | ||
@@ -120,3 +152,3 @@ const input = { | ||
## Matching Arrays | ||
### Matching Arrays | ||
``` | ||
@@ -130,3 +162,3 @@ const input = { | ||
## Array with repeating nodes | ||
### Array with repeating nodes | ||
``` | ||
@@ -149,3 +181,3 @@ const input = { | ||
## Array with unordered nodes | ||
### Array with unordered nodes | ||
``` | ||
@@ -169,3 +201,3 @@ const input = { | ||
## Array with optional nodes | ||
### Array with optional nodes | ||
``` | ||
@@ -191,20 +223,3 @@ const input = { | ||
## Nested | ||
``` | ||
const input = { | ||
inner1: { | ||
hello: "world" | ||
} | ||
} | ||
const schema = traverse({ | ||
inner1: { | ||
hello: capture() | ||
} | ||
}) | ||
//result is Match { value: { inner1: { hello: "world" } } } | ||
``` | ||
## Optional | ||
### Optional | ||
Matches if found. Does not emit a Skip() if not found. | ||
@@ -226,5 +241,4 @@ ``` | ||
## Merging with Parent (replace flag) | ||
### Nested | ||
``` | ||
const input = { | ||
@@ -237,11 +251,35 @@ inner1: { | ||
const schema = traverse({ | ||
inner1: { | ||
prop1: "HELLO" | ||
inner1: traverse({ | ||
hello: capture() | ||
} | ||
}, { replace: true }) | ||
}) | ||
}) | ||
//result is Match { value: { inner1: { hello: "world" } } } | ||
``` | ||
### Merging with Parent (replace flag) | ||
``` | ||
const input = { | ||
inner1: { | ||
hello: "world" | ||
} | ||
} | ||
const schema = traverse( | ||
{ | ||
inner1: traverse( | ||
{ | ||
hello: capture() | ||
} | ||
) | ||
}, | ||
{ replace: true } | ||
) | ||
//result is Match { value: { hello: "world" } } | ||
``` | ||
## Capturing and traversing Child Schema (Parent-Child) | ||
### Capturing and traversing Child Schema (Parent-Child) | ||
``` | ||
@@ -263,3 +301,3 @@ const input = { | ||
## Matching Any Schema: any(list) | ||
### Matching Any Schema: any(list) | ||
``` | ||
@@ -285,3 +323,3 @@ const input = { | ||
## Matching nodes deeper in the tree: deep(schema) | ||
### Matching nodes deeper in the tree: deep(schema) | ||
``` | ||
@@ -322,3 +360,3 @@ const input = { | ||
## Matching undefined or empty: empty() | ||
### Matching undefined or empty: empty() | ||
``` | ||
@@ -338,3 +376,3 @@ const input = { | ||
## Matching the existence of a node: exists() | ||
### Matching the existence of a node: exists() | ||
``` | ||
@@ -354,3 +392,3 @@ export const input = { | ||
## Matching with Regex: regex() | ||
### Matching with Regex: regex() | ||
``` | ||
@@ -366,3 +404,3 @@ const input = { | ||
## Advanced | ||
### Advanced | ||
Property Modifier. Use this if your input tree isn't a simple object. | ||
@@ -382,3 +420,3 @@ ``` | ||
## Builders | ||
### Builders | ||
Advanced features which let you: | ||
@@ -421,3 +459,3 @@ - modify the result of the capture (builder.get) | ||
## Builder Asserts | ||
### Builder Asserts | ||
Generates a Fault which will stop the matching on the tree. | ||
@@ -444,3 +482,3 @@ ``` | ||
## Builder Predicates | ||
### Builder Predicates | ||
Similar to Asserts, but returns a Skip instead of Fault. | ||
@@ -447,0 +485,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
125241
802
481