All files functional.ts

90.48% Statements 76/84
80% Branches 36/45
100% Functions 12/12
90.14% Lines 64/71

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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220    1x                   1x                                                     1x   1x 35x     1x 41x 17x     1x 81x     1x 6x 6x             2x 2x     6x     1x     6x         1x           16x 11x 10x 7x 6x 4x       1x         9x         6x     12x                   1x 1x     11x 11x       5x 5x         3x 3x           3x               11x   5x     1x     26x 26x       6x         14x             13x 13x 13x       1x 1x             5x   8x   1x         3x       3x       3x     6x     1x 1x 1x 2x   1x     1x 1x    
import { ArgType, Values, Keywords} from './types'
 
export const primitives = [
	'string',
	'number',
	'boolean',
	'bigint',
	'undefined',
	'null',
	'symbol'
]
 
export const defaultKeywords: Keywords = {
	_OR: '$or',
	_AND: '$and',
	_NOR: '$nor',
	_ALL: '$all',
	_IN: '$in',
	_NIN: '$nin',
	_EQ: '$eq',
	_NE: '$ne',
	_LT: '$lt',
	_LTE: '$lte',
	_GT: '$gt',
	_GTE: '$gte',
	// geo queries operators:
	_GEO_INTERSECTS: '$geoIntersects',
	_GEO_WITHIN: '$geoWithin',
	_NEAR: '$near',
	_NEAR_SPHERE: '$nearSphere',
	// geo shapes operators:
	_GEOMETRY: '$geometry',
	_BOX: '$box',
	_POLYGON: '$polygon',
	_CENTER: '$center',
	_CENTER_SPHERE: '$centerSphere',
	_MAX_DISTANCE: '$maxDistance',
	_MIN_DISTANCE: '$minDistance'
}
const defaultValues: Values = {}
 
export const isOperator = (key: string, keywords: object): boolean => {
    return Object.keys(keywords).includes(key)
}
 
export const isPrimitive = (val): boolean => {
	if (primitives.includes(typeof val)) return true
	else return false
}
 
export const isComputable = (key: string, values: object): boolean => {
	return Object.keys(values).includes(key)
}
 
export const isNested = (value, keywords: object, values: object): boolean => {
	Iif (typeof value !== 'object') return false
	let nested = false
	for (const key in value) {
		if (
			!isOperator(key, keywords) &&
			!isPrimitive(value[key]) &&
			!isComputable(key, values)
		) {
			nested = true
			break
		}
	}
	return nested
}
 
export const computedValue = (parent: object, values: object) => {
	for (const valueKey in values) {
		if (parent[valueKey] !== undefined) {
			return values[valueKey](parent)
		}
	}
}
 
export const argType = (
	keywords: object,
	values: object,
	key?: string,
	val?: string
): ArgType => {
	if (isOperator(key, keywords)) return 'OPERATOR'
	else if (isComputable(key, values)) return 'COMPUTED'
	else if (isPrimitive(val)) return 'PRIMITIVE'
	else if (Array.isArray(val)) return 'ARRAY'
	else if (isNested(val, keywords, values)) return 'NESTED'
	else Eif (typeof val === 'object') return 'FLAT'
	else return null
}
 
const parseNested = (
	keywords: Keywords,
	values: Values,
	key: string,
	val,
	lastResult = {}
) => {
	if (isComputable(key, values)) {
		return buildFilters(val, key, keywords, values)
	}
	let result = lastResult
 
	for (const k in val) {
		let isFinal = false
 
		// COMPUTABLE VALUE
		if (isComputable(k, values)) {
			result = { ...result, ...buildFilters(val[k], k, keywords, values) }
			return result
		}
 
		// OPERATOR
		if (isOperator(k, keywords)) {
			result = { ...result, [key]: buildFilters(val, key, keywords, values) }
			return result
		}
 
		const subkey = key + '.' + k
		const subval = val[k]
 
		// subval is COMPUTABLE VALUE
		if (isComputable(subkey, values)) {
			result = { ...result, ...buildFilters(subval, subkey, keywords, values) }
			isFinal = true
		}
 
		// subval is a PRIMITIVE
		else if (isPrimitive(subval)) {
			result[subkey] = buildFilters(subval, null, keywords, values)
			isFinal = true
		}
 
		// subval is NESTED
        else {
            for (const sk in subval) {
                const t = argType(keywords, values, sk, subval)
                if (t !== 'NESTED' && t !== 'FLAT') {
                    result[subkey] = buildFilters(subval, null, keywords, values)
                    isFinal = true
                    break
                }
            }
        }
		if (!isFinal) parseNested(keywords, values, subkey, subval, result)
	}
	return result
}
 
export const buildFilters = (
	args,
	parentKey?: string,
	Ikeywords: Keywords = {},
	Ivalues: Values = {}
) => {
	// PARENT IS A COMPUTABLE VALUE
	if (isComputable(parentKey, values)) {
		return computedValue({ [parentKey]: args }, values)
	}
 
	// NO PARENT AND ARGS IS A DIRECT VALUE
	if (!parentKey && isPrimitive(args)) {
		return args
	}
 
	// ELSE, ARGS MUST BE ON OBJECT. LET'S ITERATE:
	let filters
 
	for (const key in args) {
		if (!filters) filters = {}
		const val = args[key]
		const t = argType(keywords, values, key, val)
 
		// COMPUTED VALUE
		if (isComputable(key, values)) {
			const computed = buildFilters(val, key, keywords, values)
			filters = {
				...filters,
				...computed
			}
		}
		// OPERATOR
		else if (t === 'OPERATOR') {
			const keyword = keywords[key]
			if (Array.isArray(val)) {
				filters[keyword] = val.map(v => buildFilters(v, null, keywords, values))
			} else {
				filters[keyword] = buildFilters(val, null, keywords, values)
			}
		}
		// NESTED QUERY
		else if (t === 'NESTED' || t === 'FLAT') {
			filters = { ...filters, ...parseNested(keywords, values, key, val) }
		}
		// ARRAY
		else if (t === 'ARRAY') {
			filters[key] = val.map(v => buildFilters(v, null, keywords, values))
		}
		// ELSE
		else {
			filters[key] = buildFilters(val, null, keywords, values)
		}
	}
	return filters
}
 
export default (
	IcustomKeywords: Keywords = {},
	IcustomValues: Values = {},
	Emerge: boolean = true
) => {
	const keywords: Keywords = merge
		? { ...defaultKeywords, ...customKeywords }
		: customKeywords
	const values: Values = merge ? { ...defaultValues, ...customValues } : customValues
	return (args: object): object => buildFilters(args, null, keywords, values)
}