Skip to content

Commit

Permalink
Allow empty string on find command
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrg committed May 11, 2020
1 parent e039f3c commit 2dcbcee
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
9 changes: 9 additions & 0 deletions changelog/60-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
issue: '#60'
type: change
audience: user
date: '2020-05-07'
components:
- cli
---
# Show all note files when running `find` without argument
9 changes: 4 additions & 5 deletions packages/snage/src/command/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import {pipe} from 'fp-ts/lib/pipeable';
import {Config} from '../config/type';
import {Note} from '../note/note';

export const find: yargs.CommandModule<DefaultCli, DefaultCli> = {
command: 'find',
describe: 'Find notes matching <condition>',
builder: (y) => y.usage('<condition>'),
handler: async ({_: [, condition]}) => {
export const find: yargs.CommandModule<DefaultCli, DefaultCli & {condition?: string}> = {
command: 'find [condition]',
describe: 'Find notes matching [condition]',
handler: async ({condition = ''}) => {
return pipe(
TE.fromEither(getConfig()),
TE.chain((config) =>
Expand Down
22 changes: 7 additions & 15 deletions packages/snage/src/command/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as IO from 'fp-ts/lib/IO';
import * as T from 'fp-ts/lib/Task';
import * as E from 'fp-ts/lib/Either';
import * as A from 'fp-ts/lib/Array';
import * as B from 'fp-ts/lib/boolean';
import {identity} from 'fp-ts/lib/function';
import {Config} from '../config/type';
import {convertToApiNote} from '../note/convertapi';
Expand Down Expand Up @@ -60,20 +59,13 @@ export const startExpress = (port: number) => ([config, notes]: [Config, Note[]]

app.get('/note', ({query: {query}}, res) => {
pipe(
query && query !== '',
B.fold(
() => E.right(notes),
() =>
pipe(
parser(query),
E.bimap(
(e): Response => ({status: 400, body: e}),
(expression) => {
const matcher = createMatcher(expression, config.fields);
return notes.filter((note) => matcher(note.values));
}
)
)
parser(query),
E.bimap(
(e): Response => ({status: 400, body: e}),
(expression) => {
const matcher = createMatcher(expression, config.fields);
return notes.filter((note) => matcher(note.values));
}
),
E.map(A.sort(config.standard.sort)),
E.map(A.map((note) => convertToApiNote(note, config.fields))),
Expand Down
6 changes: 1 addition & 5 deletions packages/snage/src/command/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,8 @@ export const updateNotes = ({
};

const filterNotes = (condition: string | undefined, fields: Field[], notes: Note[]): E.Either<string, Note[]> => {
if (!condition) {
return E.right(notes);
}

return pipe(
createParser(fields)(condition),
createParser(fields)(condition ?? ''),
E.bimap(
(e) => `Invalid expression ${condition} ${JSON.stringify(e)}`,
(expression) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/snage/src/query/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ interface Matcher {
}

export const createMatcher = (e: Expression, fields: Field[]): Matcher => {
if (e === true) {
return () => true;
}
if (Array.isArray(e)) {
const [left, op, right] = e;
if (op === 'or') {
Expand Down
5 changes: 4 additions & 1 deletion packages/snage/src/query/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type SingleExpression = {
export const StatusOP = '__status__' as const;
export type Operator = '=' | '<=' | '>=' | '!=' | '<' | '>' | '~' | '~~' | typeof StatusOP;

export type Expression = SingleExpression | [Expression, 'or' | 'and', Expression];
export type Expression = true | SingleExpression | [Expression, 'or' | 'and', Expression];

export enum StatusValue {
Present = 'present',
Expand Down Expand Up @@ -112,6 +112,9 @@ export const createParser = (fields: Field[]): ((q: string) => Either<ParseError
expression: (r) => P.alt(r.orAndExpression, r.braceExpression, r.singleExpression),
}).expression;
return (s) => {
if (s.trim() === '') {
return right(true);
}
const result = expression.parse(s);
if (result.status) {
return right(result.value);
Expand Down

0 comments on commit 2dcbcee

Please sign in to comment.