aboutsummaryrefslogtreecommitdiff
path: root/src/matcher/options.ts
blob: 5b7c2d9ebd490e4a21a513d3d85917284edd27e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { default as types } from './types';

export function rough(template: Object, value: Object): boolean {
  for (let k of Object.keys(template)) {
    if (!template[k](value[k])) {
      return false;
    }
  }
  return true;
}

export function match(template: Object): (Object) => boolean {
  return rough.bind(null, template);
}

export function exact(template: Object): (Object) => boolean {
  let m = match(template);
  let revm = (value: Object) => rough(value, template);
  return (v) => m(v) && revm(v);
}

export function optional(template: Function): (any) => boolean {
  return (v) => types.undefined(v)
             || template(v);
}

export function alternative(templates: any[]) {
  if (templates.length === 0) {
    return () => false;
  } else {
    let t = templates.map((x) => {
      if (types.function(x)) {
        return x;
      } else if (types.object(x)) {
        return match(x);
      } else {
        return (v) => x === v;
      }
    });
    return (v) => t.some((x) => x(v));
  }
}

export function array(template: Object): (Array) => boolean {
  let m = match(template);
  return (v) => v.every(m);
}