All files / xstate/src utils.ts

95.58% Statements 108/113
84.48% Branches 49/58
100% Functions 23/23
95.41% Lines 104/109

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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269                1x     4665x 1770x     2895x     1x 52792x     1x     336x   168x 168x   168x 51x 49x       2x     117x 32x     85x 66x 4x     62x       1x     69x 69x                 1x 2x 2x                       1x       3664x 3664x 1157x     2507x           1x       3640x       3640x 845x     2795x 1025x     1770x   1770x     1x 2871x 2241x     630x 630x   630x 808x 605x   203x 203x       630x     1x                 13143x   13143x 18258x     13141x     1x         1389x   1389x 3716x   3716x 118x     3598x     1389x             192x     192x   192x 46x     192x             1x       26x 26x   32x 32x     26x       1x     2143x       2143x 1386x     757x   987x 1068x         757x     1x 37x   37x 28x     11x 11x   11x 13x   13x 11x 11x   2x 2x       9x     1x 14176x     1x 8014x 3895x   4119x 3056x   1063x    
import {
  Event,
  StateValue,
  ActionType,
  Action,
  EventObject,
  StateInterface
} from './types';
import { STATE_DELIMITER } from './constants';
 
function isState(state: object | string): state is StateInterface {
  if (typeof state === 'string') {
    return false;
  }
 
  return 'value' in state && 'tree' in state && 'history' in state;
}
 
export function keys<T extends object>(value: T): Array<keyof T & string> {
  return Object.keys(value) as Array<keyof T & string>;
}
 
export function matchesState(
  parentStateId: StateValue,
  childStateId: StateValue,
  Edelimiter: string = STATE_DELIMITER
): boolean {
  const parentStateValue = toStateValue(parentStateId, delimiter);
  const childStateValue = toStateValue(childStateId, delimiter);
 
  if (typeof childStateValue === 'string') {
    if (typeof parentStateValue === 'string') {
      return childStateValue === parentStateValue;
    }
 
    // Parent more specific than child
    return false;
  }
 
  if (typeof parentStateValue === 'string') {
    return parentStateValue in childStateValue;
  }
 
  return keys(parentStateValue).every(key => {
    if (!(key in childStateValue)) {
      return false;
    }
 
    return matchesState(parentStateValue[key], childStateValue[key]);
  });
}
 
export function getEventType<TEvents extends EventObject = EventObject>(
  event: Event<TEvents>
): TEvents['type'] {
  try {
    return typeof event === 'string' || typeof event === 'number'
      ? `${event}`
      : (event as TEvents).type;
  } catch (e) {
    throw new Error(
      'Events must be strings or objects with a string event.type property.'
    );
  }
}
export function getActionType(action: Action<any>): ActionType {
  try {
    return typeof action === 'string' || typeof action === 'number'
      ? `${action}`
      : typeof action === 'function'
        ? action.name
        : action.type;
  } catch (e) {
    throw new Error(
      'Actions must be strings or objects with a string action.type property.'
    );
  }
}
 
export function toStatePath(
  stateId: string | string[],
  delimiter: string
): string[] {
  try {
    if (Array.isArray(stateId)) {
      return stateId;
    }
 
    return stateId.toString().split(delimiter);
  } catch (e) {
    throw new Error(`'${stateId}' is not a valid state path.`);
  }
}
 
export function toStateValue(
  stateValue: StateInterface<any> | StateValue | string[],
  delimiter: string
): StateValue {
  Iif (isState(stateValue)) {
    return stateValue.value;
  }
 
  if (Array.isArray(stateValue)) {
    return pathToStateValue(stateValue);
  }
 
  if (typeof stateValue !== 'string' && !isState(stateValue)) {
    return stateValue as StateValue;
  }
 
  const statePath = toStatePath(stateValue as string, delimiter);
 
  return pathToStateValue(statePath);
}
 
export function pathToStateValue(statePath: string[]): StateValue {
  if (statePath.length === 1) {
    return statePath[0];
  }
 
  const value = {};
  let marker = value;
 
  for (let i = 0; i < statePath.length - 1; i++) {
    if (i === statePath.length - 2) {
      marker[statePath[i]] = statePath[i + 1];
    } else {
      marker[statePath[i]] = {};
      marker = marker[statePath[i]];
    }
  }
 
  return value;
}
 
export function mapValues<T, P>(
  collection: { [key: string]: T },
  iteratee: (
    item: T,
    key: string,
    collection: { [key: string]: T },
    i: number
  ) => P
): { [key: string]: P } {
  const result = {};
 
  keys(collection).forEach((key, i) => {
    result[key] = iteratee(collection[key], key, collection, i);
  });
 
  return result;
}
 
export function mapFilterValues<T, P>(
  collection: { [key: string]: T },
  iteratee: (item: T, key: string, collection: { [key: string]: T }) => P,
  predicate: (item: T) => boolean
): { [key: string]: P } {
  const result = {};
 
  keys(collection).forEach(key => {
    const item = collection[key];
 
    if (!predicate(item)) {
      return;
    }
 
    result[key] = iteratee(item, key, collection);
  });
 
  return result;
}
 
/**
 * Retrieves a value at the given path.
 * @param props The deep path to the prop of the desired value
 */
export const path = <T extends Record<string, any>>(props: string[]): any => (
  object: T
): any => {
  let result: T = object;
 
  for (const prop of props) {
    result = result[prop as keyof typeof result];
  }
 
  return result;
};
 
/**
 * Retrieves a value at the given path via the nested accessor prop.
 * @param props The deep path to the prop of the desired value
 */
export function nestedPath<T extends Record<string, any>>(
  props: string[],
  accessorProp: keyof T
): (object: T) => T {
  return object => {
    let result: T = object;
 
    for (const prop of props) {
      result = result[accessorProp][prop];
    }
 
    return result;
  };
}
 
export const toStatePaths = (
  stateValue: StateValue | undefined
): string[][] => {
  Iif (!stateValue) {
    return [[]];
  }
 
  if (typeof stateValue === 'string') {
    return [[stateValue]];
  }
 
  const result = flatten(
    keys(stateValue).map(key => {
      return toStatePaths(stateValue[key]).map(subPath => {
        return [key].concat(subPath);
      });
    })
  );
 
  return result;
};
 
export const pathsToStateValue = (paths: string[][]): StateValue => {
  const result: StateValue = {};
 
  if (paths && paths.length === 1 && paths[0].length === 1) {
    return paths[0][0];
  }
 
  for (const currentPath of paths) {
    let marker = result;
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0; i < currentPath.length; i++) {
      const subPath = currentPath[i];
 
      if (i === currentPath.length - 2) {
        marker[subPath] = currentPath[i + 1];
        break;
      }
      marker[subPath] = marker[subPath] || {};
      marker = marker[subPath] as {};
    }
  }
 
  return result;
};
 
export function flatten<T>(array: T[][]): T[] {
  return ([] as T[]).concat(...array);
}
 
export function toArray<T>(value: T[] | T | undefined): T[] {
  if (Array.isArray(value)) {
    return value;
  }
  if (value === undefined) {
    return [];
  }
  return [value];
}