Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript refactor #97

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
10 changes: 0 additions & 10 deletions .eslintrc

This file was deleted.

2 changes: 0 additions & 2 deletions .gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions .npmignore

This file was deleted.

6 changes: 0 additions & 6 deletions .travis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
36 changes: 0 additions & 36 deletions Gruntfile.js

This file was deleted.

85 changes: 41 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,54 @@
# jmespath.js
# jmespath.ts

[![Build Status](https://travis-ci.org/jmespath/jmespath.js.png?branch=master)](https://travis-ci.org/jmespath/jmespath.js)
jmespath.ts is a TypeScript implementation of JMESPath, which is a query
language for JSON. It will take a JSON document and transform it into another
JSON document through a JMESPath expression.

jmespath.js is a javascript implementation of JMESPath,
which is a query language for JSON. It will take a JSON
document and transform it into another JSON document
through a JMESPath expression.
Using jmespath.ts is really easy. There's a single class you use, `JmesPath`:

Using jmespath.js is really easy. There's a single function
you use, `jmespath.search`:
```ts
import { JmesPath } from "jsr:@halvardm/jmespath";


```
> var jmespath = require('jmespath');
> jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar.baz[2]")
2
const jp = new JmesPath({ foo: { bar: { baz: [0, 1, 2, 3, 4] } } });
console.log(jp.search("foo.bar.baz[2]")); // 2
```

In the example we gave the ``search`` function input data of
`{foo: {bar: {baz: [0, 1, 2, 3, 4]}}}` as well as the JMESPath
expression `foo.bar.baz[2]`, and the `search` function evaluated
the expression against the input data to produce the result ``2``.

The JMESPath language can do a lot more than select an element
from a list. Here are a few more examples:

```
> jmespath.search({foo: {bar: {baz: [0, 1, 2, 3, 4]}}}, "foo.bar")
{ baz: [ 0, 1, 2, 3, 4 ] }

> jmespath.search({"foo": [{"first": "a", "last": "b"},
{"first": "c", "last": "d"}]},
"foo[*].first")
[ 'a', 'c' ]

> jmespath.search({"foo": [{"age": 20}, {"age": 25},
{"age": 30}, {"age": 35},
{"age": 40}]},
"foo[?age > `30`]")
[ { age: 35 }, { age: 40 } ]
The JMESPath language can do a lot more than select an element from a list. Here
are a few more examples:

```ts
import { JmesPath } from "jsr:@halvardm/jmespath";

const jp = new JmesPath({ foo: { bar: { baz: [0, 1, 2, 3, 4] } } });
console.log(jp.search("foo.bar"));
// { baz: [ 0, 1, 2, 3, 4 ] }

const jp = new JmesPath({
foo: [
{ first: "a", last: "b" },
{ first: "c", last: "d" },
],
});
console.log(jp.search("foo[*].first"));
// [ 'a', 'c' ]

const jp = new JmesPath({
foo: [{ age: 20 }, { age: 25 }, { age: 30 }, { age: 35 }, { age: 40 }],
});
console.log(jp.search("foo[?age > `30`]"));
// [ { age: 35 }, { age: 40 } ]
```

## More Resources

The example above only show a small amount of what
a JMESPath expression can do. If you want to take a
tour of the language, the *best* place to go is the
The example above only show a small amount of what a JMESPath expression can do.
If you want to take a tour of the language, the _best_ place to go is the
[JMESPath Tutorial](http://jmespath.org/tutorial.html).

One of the best things about JMESPath is that it is
implemented in many different programming languages including
python, ruby, php, lua, etc. To see a complete list of libraries,
check out the [JMESPath libraries page](http://jmespath.org/libraries.html).
One of the best things about JMESPath is that it is implemented in many
different programming languages including python, ruby, php, lua, etc. To see a
complete list of libraries, check out the
[JMESPath libraries page](http://jmespath.org/libraries.html).

And finally, the full JMESPath specification can be found
on the [JMESPath site](http://jmespath.org/specification.html).
And finally, the full JMESPath specification can be found on the
[JMESPath site](http://jmespath.org/specification.html).
2 changes: 0 additions & 2 deletions artifacts/jmespath.min.js

This file was deleted.

24 changes: 24 additions & 0 deletions bin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env -S deno run

import { JmesPath } from "./mod.ts";

if (Deno.args.length < 1) {
console.error("Must provide a jmespath expression.");
Deno.exit(1);
}

const inputJSON: string[] = [];

const decoder = new TextDecoder();
for await (const chunk of Deno.stdin.readable) {
const text = decoder.decode(chunk);
inputJSON.push(text);
}

const expression = Deno.args[0];

const parsedInput = JSON.parse(inputJSON.join(""));

const res = new JmesPath(parsedInput).search(expression);

console.info(JSON.stringify(res));
24 changes: 0 additions & 24 deletions bower.json

This file was deleted.

20 changes: 20 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@halvardm/jmespath",
"version": "0.17.0",
"exports": {
".": "./mod.ts",
"./bin": "./bin.ts"
},
"tasks": {
"test": "deno test --allow-read",
"test:inspect": "deno task test --inspect-wait"
},
"license": "Apache-2.0",
"imports": {
"@std/assert": "jsr:@std/assert@1",
"@std/path": "jsr:@std/path@^1"
},
"fmt": {
"exclude": ["test/compliance"]
}
}
38 changes: 38 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading