Skip to content

Commit

Permalink
feat: add sortBy function
Browse files Browse the repository at this point in the history
docs(gh-pages): documentation for sortBy

style(interpreter-test-js): fix style issues
  • Loading branch information
kantord committed Oct 12, 2018
1 parent 8ae8fb8 commit 6f57f1d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# emuto ![build](https://img.shields.io/travis/kantord/emuto/master.svg) ![Codecov](https://img.shields.io/codecov/c/github/kantord/emuto/master.svg) ![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/emuto.svg)
# emuto ![build](https://img.shields.io/travis/kantord/emuto/master.svg) ![Codecov](https://img.shields.io/codecov/c/github/kantord/emuto/master.svg) ![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/emuto.svg)

emuto is a lightweight JSON processor inspired by and nearly porting [jq](https://stedolan.github.io/jq/)

Expand Down Expand Up @@ -100,6 +100,16 @@ Output:

Alternatively, you can also use this syntax: `["Hello", "World!"] | map \ {"word": $}`

#### `sortBy \`

Takes an array as input and sorts it using the supplied function.

For example:

```
sortBy $ => .age | map $ => .name
```

### Chaining filters

You can combine two filters by using the pipe syntax, e. q. `.foo | .bar`
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ exports[`interpreter correct target code null 1`] = `"(function(_) { return (fun

exports[`interpreter correct target code null: true 1`] = `"(function(_) { return (function(input) { return [null,true]})})"`;

exports[`interpreter correct target code sortBy \\.age | map \\.name 1`] = `"(function(_) { return (function(input) { return (function (input) {return (_.map(function(input) {return input.name})(input))})((_.sortBy(function(input) {return input.age})(input)))})})"`;

exports[`interpreter correct target code true 1`] = `"(function(_) { return (function(input) { return true})})"`;

exports[`interpreter correct target tree $ 1`] = `
Expand Down Expand Up @@ -1402,6 +1404,43 @@ Object {
}
`;

exports[`interpreter correct target tree sortBy \\.age | map \\.name 1`] = `
Object {
"status": true,
"value": Object {
"name": "pipe",
"value": Object {
"left": Object {
"name": "functionCallLambda",
"value": Object {
"left": Object {
"name": "identifier",
"value": "sortBy",
},
"right": Object {
"name": "inputProp",
"value": ".age",
},
},
},
"right": Object {
"name": "functionCallLambda",
"value": Object {
"left": Object {
"name": "identifier",
"value": "map",
},
"right": Object {
"name": "inputProp",
"value": ".name",
},
},
},
},
},
}
`;

exports[`interpreter correct target tree true 1`] = `
Object {
"status": true,
Expand Down
23 changes: 22 additions & 1 deletion src/__tests__/builtins.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import builtIns from '../builtins'

const { projection, join, map } = builtIns
const { projection, join, map, sortBy } = builtIns

describe('built ins', () => {
describe('projection', () => {
Expand Down Expand Up @@ -45,4 +45,25 @@ describe('built ins', () => {
expect(map(foo)(['Hello', 'World'])).toEqual(['foo', 'foo'])
})
})

describe('sortBy', () => {
it('returns correct value', () => {
const id = a => a; // eslint-disable-line
const foo = a => a.foo; // eslint-disable-line
expect(sortBy(id)(['c', 'a', 'b'])).toEqual(['a', 'b', 'c'])
expect(sortBy(foo)([{ foo: 3 }, { foo: -2 }, { foo: 0 }])).toEqual([
{ foo: -2 },
{ foo: 0 },
{ foo: 3 }
])
})

it('does not mutate original array', () => {
const id = a => a; // eslint-disable-line
const input = ['c', 'a', 'b']
const originalValue = input.slice()
sortBy(id)(input)
expect(input).toEqual(originalValue)
})
})
})
5 changes: 5 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ const tests = [
{
sourceCode: `{"foo": [{"bar": "baz"}]} | $.foo[0].bar`,
output: 'baz'
},
{
sourceCode: `sortBy \\.age | map \\.name`,
output: ['John', 'Mary'],
input: [{ name: 'John', age: 11 }, { name: 'Mary', age: 33 }]
}
]

Expand Down
11 changes: 10 additions & 1 deletion src/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ export default {
): string => input.join(separator),
map: (f: mixed => mixed): ((Array<mixed>) => Array<mixed>) => (
input: Array<mixed>
): Array<mixed> => input.map(f)
): Array<mixed> => input.map(f),
sortBy: (f: <T>(mixed) => T): ((Array<mixed>) => Array<mixed>) => (
input: Array<mixed>
): Array<mixed> =>
input
.slice()
.sort(
(a: mixed, b: mixed): 1 | 0 | -1 =>
f(a) < f(b) ? -1 : f(a) > f(b) ? 1 : 0
)
}

0 comments on commit 6f57f1d

Please sign in to comment.