All files / src builtins.js

100% Statements 67/67
97.22% Branches 35/36
100% Functions 43/43
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188                          2x 70x   2x 37x   2x   2x 28x       37x 37x       2x   29x     33x           2x           30x 30x     47x 28x 28x 28x 23x   5x       47x 5x     47x 14x 14x 14x 14x 13x   1x         30x     2x   29x     29x             8x                     32x 21x 8x       2x   2x   3x   3x   21x     21x       7x     6x     6x     16x         4x   4x   4x   4x     36x               33x     4x       2x 6x   4x   2x   2x   2x   2x   2x     2x   3x 3x          
// @flow
 
import combinations from 'combinations-generator'
import { product } from 'cartesian-product-generator'
import type { ObjectProjectionItemType } from './types'
 
type ProjectableType = Array<mixed> & { [string]: mixed };
type ProjectionRuleType = number & string;
type ProjectionRulesType = Array<ProjectionRuleType>;
type ProjectableObjectType = {
  [string]: ProjectableObjectType
};
 
const convertUndefined = (value: ?mixed): mixed | null =>
  value === undefined ? null : value
 
const handleOptional = (value: ?mixed, f: mixed => mixed): mixed =>
  convertUndefined(value) === null ? null : f(value)
 
const parseJSON = JSON.parse
 
const objectify = (input: Array<[string, mixed]>): { [string]: mixed } =>
  input.reduce(function (
    a: { [string]: mixed },
    b: [string, mixed]
  ): { [string]: mixed } {
    a[b[0]] = b[1]
    return a
  },
  {})
 
const handleProjectionItem = (
  projectable: ProjectableType
): (ProjectionRuleType => mixed) => (
  projectionRule: ProjectionRuleType
): mixed =>
  Number.isInteger(projectionRule)
    ? convertUndefined(projectable.slice(projectionRule)[0])
    : Array.isArray(projectionRule)
      ? convertUndefined(projectable.slice(...projectionRule))
      : convertUndefined(projectable[projectionRule])
 
const __objectProjection__ = (
  left: ProjectableObjectType,
  right: Array<ObjectProjectionItemType>,
  optional: boolean,
  _: Object
): mixed => {
  const newObject = {}
  right.forEach((rule: ObjectProjectionItemType) => {
    let key
    let finalKey
    if (rule.type === 'SimpleItem') {
      key = rule.value
      finalKey = rule.alias || key
      if (key in left) {
        newObject[finalKey] = left[key]
      } else {
        newObject[finalKey] = null
      }
    }
 
    if (rule.type === 'FragmentItem') {
      Object.assign(newObject, _[rule.value](left))
    }
 
    if (rule.type === 'RecursiveItem') {
      key = rule.name
      finalKey = rule.alias || key
      const rules = rule.value.value
      if (key in left) {
        newObject[finalKey] = __objectProjection__(left[key], rules, false, _)
      } else {
        newObject[finalKey] = null
      }
    }
  })
 
  return newObject
}
 
const handleProjection = (
  projectable: ProjectableType
): (ProjectionRulesType => Array<mixed> & mixed) => (
  projectionRules: ProjectionRulesType
): Array<mixed> & mixed =>
  projectionRules.map(handleProjectionItem(projectable))
 
export default {
  objectify,
  __opt__: handleOptional,
 
  __spread__: (input: mixed): mixed =>
    Array.isArray(input)
      ? input
      : typeof input === 'string' || input instanceof String
        ? input.split('')
        : Object.entries(input),
 
  projection: (
    left: ProjectableType,
    right: ProjectionRulesType,
    optinal: boolean
  ): mixed =>
    right.length === 1
      ? handleOptional(left, (): mixed => handleProjection(left)(right)[0])
      : handleOptional(left, (): mixed => handleProjection(left)(right)),
 
  __objectProjection__,
 
  split: (separator: string): (string => Array<string>) => (
    input: string
  ): Array<string> => input.split(separator),
 
  join: (separator: string): ((Array<string>) => string) => (
    input: Array<string>
  ): string => input.join(separator),
 
  map: (f: any): ((Array<mixed>) => any) => (
    input: Array<mixed> | mixed
  ): Array<mixed> | mixed =>
    Array.isArray(input)
      ? input.map(f)
      : objectify(
        // eslint-disable-next-line standard/array-bracket-even-spacing
        Object.entries(input).map(([key, value]: any): any => f(key)(value))
      ),
 
  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
      ),
 
  has: (
    key: string | number
  ): ((Array<mixed> | { [mixed]: mixed }) => boolean) => (
    input: Array<mixed> | { [mixed]: mixed }
  ): boolean => key in input,
 
  filter: (f: mixed => boolean): ((Array<mixed>) => Array<mixed>) => (
    input: Array<mixed>
  ): Array<mixed> => input.filter(f),
 
  get: function (variable: string): mixed {
    return this[variable]
  },
 
  __assign__: function (
    variable: string,
    value: mixed,
    context: { [string]: mixed }
  ): { [string]: mixed } {
    return Object.assign({}, context, { [variable]: value })
  },
 
  reverse: (input: Array<mixed>): Array<mixed> => input.slice().reverse(),
 
  reduce: ([f, x]: [(mixed) => mixed => mixed, mixed]): ((
    Array<mixed>
  ) => mixed) => (input: Array<mixed>): mixed =>
    input.reduce((a: mixed, b: mixed): mixed => f(a)(b), x),
 
  length: (input: Array<mixed>): number => input.length,
 
  keys: (input: { [string]: mixed }): Array<string> => Object.keys(input),
 
  values: (input: { [string]: mixed }): Array<mixed> => Object.values(input),
  entries: (input: { [string]: mixed }): Array<[string, mixed]> =>
    Object.entries(input),
 
  combinations: (r: number): Array<mixed> | (string => Array<Array<mixed>>) => (
    input: Array<mixed> | string
  ): Array<Array<mixed>> => Array.from(combinations(input, r)),
 
  product: (input: Array<mixed> | string): Array<Array<mixed>> =>
    Array.from(product(...input)),
 
  error: (message: string): (mixed => void) => (input: mixed) => {
    throw new Error(message)
  },
 
  parseJSON
}