All files utils.ts

100% Statements 43/43
96.88% Branches 31/32
100% Functions 7/7
100% Lines 36/36

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                            7x 302x 301x                 7x 12x 11x 11x 11x               7x     184x   168x 168x 168x   58x 58x   58x             7x 3963x               7x 119x 119x 23x                             7x   107x 50x       57x 139x 139x               7x 77x 23x   54x 54x 54x 39x 12x    
 
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
import { asciifold } from './diacritics.ts';
 
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
import * as T from './types.ts';
 
 
/**
 * A property getter resolving dot-notation
 * @param  {Object}  obj     The root object to fetch property on
 * @param  {String}  name    The optionally dotted property name to fetch
 * @return {Object}          The resolved property value
 */
export const getAttr = (obj:{[key:string]:any}, name:string ) => {
    if (!obj ) return;
    return obj[name];
};
 
/**
 * A property getter resolving dot-notation
 * @param  {Object}  obj     The root object to fetch property on
 * @param  {String}  name    The optionally dotted property name to fetch
 * @return {Object}          The resolved property value
 */
export const getAttrNesting = (obj:{[key:string]:any}, name:string ) => {
    if (!obj ) return;
    var part, names = name.split(".");
	while( (part = names.shift()) && (obj = obj[part]));
    return obj;
};
 
/**
 * Calculates how close of a match the
 * given value is against a search token.
 *
 */
export const scoreValue = (value:string, token:T.Token, weight:number ):number => {
	var score, pos;
 
	if (!value) return 0;
 
	value = value + '';
	pos = value.search(token.regex);
	if (pos === -1) return 0;
 
	score = token.string.length / value.length;
	if (pos === 0) score += 0.5;
 
	return score * weight;
};
 
/**
 *
 * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
 */
export const escape_regex = (str:string):string => {	
	return (str + '').replace(/([\$\(\)\*\+\.\?\[\]\^\{\|\}\\])/gu, '\\$1');
};
 
 
/**
 * Cast object property to an array if it exists and has a value
 *
 */
export const propToArray = (obj:{[key:string]:any}, key:string) => {
	var value = obj[key];
	if( value && !Array.isArray(value) ){
		obj[key] = [value];
	}
}
 
 
/**
 * Iterates over arrays and hashes.
 *
 * ```
 * iterate(this.items, function(item, id) {
 *    // invoked for each item
 * });
 * ```
 *
 */
export const iterate = (object:[]|{[key:string]:any}, callback:(value:any,key:number|string)=>any) => {
 
	if ( Array.isArray(object)) {
		object.forEach(callback);
 
	}else{
 
		for (var key in object) {
			Eif (object.hasOwnProperty(key)) {
				callback(object[key], key);
			}
		}
	}
};
 
 
 
export const cmp = (a:number|string, b:number|string) => {
	if (typeof a === 'number' && typeof b === 'number') {
		return a > b ? 1 : (a < b ? -1 : 0);
	}
	a = asciifold(a + '').toLowerCase();
	b = asciifold(b + '').toLowerCase();
	if (a > b) return 1;
	if (b > a) return -1;
	return 0;
};