|
| 1 | +import { |
| 2 | + Element, |
| 3 | + StringElement, |
| 4 | + NumberElement, |
| 5 | + NullElement, |
| 6 | + BooleanElement, |
| 7 | + ArrayElement, |
| 8 | + ObjectElement, |
| 9 | + MemberElement, |
| 10 | +} from 'minim'; |
| 11 | +import { either, allPass, is, both, has, curry, pathSatisfies, pathEq } from 'ramda'; |
| 12 | +import { invokeArgs, isFunction } from 'ramda-adjunct'; |
| 13 | + |
| 14 | +// hasGetter :: String -> Object -> Boolean |
| 15 | +const hasGetter = curry((name: string, obj: Record<string, unknown>): boolean => { |
| 16 | + // @ts-ignore |
| 17 | + const descriptor = Object.getOwnPropertyDescriptor(obj, name); |
| 18 | + |
| 19 | + return pathSatisfies(isFunction, ['get'], descriptor); |
| 20 | +}); |
| 21 | + |
| 22 | +// hasMethod :: String -> Object -> Boolean |
| 23 | +const hasMethod = curry((name: string, obj: Record<string, unknown>): boolean => |
| 24 | + pathSatisfies(isFunction, [name], obj), |
| 25 | +); |
| 26 | + |
| 27 | +// hasBasicElementProps :: Object -> Boolean |
| 28 | +const hasBasicElementProps = both(has('element'), has('content')); |
| 29 | + |
| 30 | +// primitiveEq :: * -> Object -> Boolean |
| 31 | +const primitiveEq = curry( |
| 32 | + (val: undefined, obj: Record<string, unknown>): boolean => |
| 33 | + invokeArgs(['primitive'], [], obj) === val, |
| 34 | +); |
| 35 | + |
| 36 | +// isElementType :: String -> Boolean |
| 37 | +const isElementType = pathEq(['element']); |
| 38 | + |
| 39 | +// @ts-ignore |
| 40 | +export const isElement = either(is(Element), both(hasBasicElementProps, primitiveEq(undefined))); |
| 41 | + |
| 42 | +export const isStringElement = either( |
| 43 | + is(StringElement), |
| 44 | + allPass([ |
| 45 | + hasBasicElementProps, |
| 46 | + // @ts-ignore |
| 47 | + isElementType('string'), |
| 48 | + // @ts-ignore |
| 49 | + primitiveEq('string'), |
| 50 | + // @ts-ignore |
| 51 | + hasGetter('length'), |
| 52 | + ]), |
| 53 | +); |
| 54 | + |
| 55 | +export const isNumberElement = either( |
| 56 | + is(NumberElement), |
| 57 | + // @ts-ignore |
| 58 | + allPass([hasBasicElementProps, isElementType('number'), primitiveEq('number')]), |
| 59 | +); |
| 60 | + |
| 61 | +export const isNullElement = either( |
| 62 | + is(NullElement), |
| 63 | + // @ts-ignore |
| 64 | + allPass([hasBasicElementProps, isElementType('null'), primitiveEq('null')]), |
| 65 | +); |
| 66 | + |
| 67 | +export const isBooleanElement = either( |
| 68 | + is(BooleanElement), |
| 69 | + // @ts-ignore |
| 70 | + allPass([hasBasicElementProps, isElementType('boolean'), primitiveEq('boolean')]), |
| 71 | +); |
| 72 | + |
| 73 | +export const isArrayElement = either( |
| 74 | + is(ArrayElement), |
| 75 | + allPass([ |
| 76 | + hasBasicElementProps, |
| 77 | + // @ts-ignore |
| 78 | + isElementType('array'), |
| 79 | + // @ts-ignore |
| 80 | + primitiveEq('array'), |
| 81 | + // @ts-ignore |
| 82 | + hasMethod('push'), |
| 83 | + // @ts-ignore |
| 84 | + hasMethod('unshift'), |
| 85 | + // @ts-ignore |
| 86 | + hasMethod('map'), |
| 87 | + // @ts-ignore |
| 88 | + hasMethod('reduce'), |
| 89 | + ]), |
| 90 | +); |
| 91 | + |
| 92 | +export const isObjectElement = either( |
| 93 | + is(ObjectElement), |
| 94 | + allPass([ |
| 95 | + hasBasicElementProps, |
| 96 | + // @ts-ignore |
| 97 | + isElementType('object'), |
| 98 | + // @ts-ignore |
| 99 | + primitiveEq('object'), |
| 100 | + // @ts-ignore |
| 101 | + hasMethod('keys'), |
| 102 | + // @ts-ignore |
| 103 | + hasMethod('values'), |
| 104 | + // @ts-ignore |
| 105 | + hasMethod('items'), |
| 106 | + ]), |
| 107 | +); |
| 108 | + |
| 109 | +export const isMemberElement = either( |
| 110 | + is(MemberElement), |
| 111 | + allPass([ |
| 112 | + hasBasicElementProps, |
| 113 | + // @ts-ignore |
| 114 | + isElementType('member'), |
| 115 | + // @ts-ignore |
| 116 | + primitiveEq(undefined), |
| 117 | + // @ts-ignore |
| 118 | + hasGetter('key'), |
| 119 | + // @ts-ignore |
| 120 | + hasGetter('value'), |
| 121 | + ]), |
| 122 | +); |
0 commit comments