2018-08-01 07:33:39 -06:00
|
|
|
import { ResourceName } from "farmbot";
|
2017-06-29 12:54:02 -06:00
|
|
|
import { Dictionary } from "farmbot/dist";
|
|
|
|
import { betterCompact } from "../util";
|
2019-02-04 07:32:26 -07:00
|
|
|
import { isArray } from "lodash";
|
2018-04-25 11:45:22 -06:00
|
|
|
import { ResourceIndex } from "./interfaces";
|
2018-11-08 09:41:09 -07:00
|
|
|
import { joinKindAndId } from "./reducer_support";
|
2017-06-29 12:54:02 -06:00
|
|
|
|
2017-08-02 09:14:08 -06:00
|
|
|
let count = 0;
|
2017-06-29 12:54:02 -06:00
|
|
|
export function generateUuid(id: number | undefined, kind: ResourceName) {
|
2017-08-02 14:05:33 -06:00
|
|
|
return `${joinKindAndId(kind, id)}.${count++}`;
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function arrayWrap<T>(input: T | (T[])): T[] {
|
2019-02-04 07:32:26 -07:00
|
|
|
return isArray(input) ? input : [input];
|
2017-06-29 12:54:02 -06:00
|
|
|
}
|
2018-10-30 12:16:06 -06:00
|
|
|
|
|
|
|
/** For when you have an array that is guaranteed to have a length of 1 */
|
2018-10-30 07:59:55 -06:00
|
|
|
export function arrayUnwrap<T>(input: T | T[]): T {
|
2019-02-04 07:32:26 -07:00
|
|
|
return isArray(input) ? input[0] : input;
|
2018-10-30 07:59:55 -06:00
|
|
|
}
|
2017-06-29 12:54:02 -06:00
|
|
|
|
|
|
|
export function entries<T>(input: Dictionary<T | undefined>): T[] {
|
2017-08-28 05:49:13 -06:00
|
|
|
const x = Object.keys(input).map(key => input[key]);
|
|
|
|
const y = betterCompact(x);
|
2017-06-29 12:54:02 -06:00
|
|
|
return y;
|
|
|
|
}
|
2018-04-25 11:45:22 -06:00
|
|
|
|
|
|
|
export function hasId(ri: ResourceIndex, k: ResourceName, id: number): boolean {
|
|
|
|
return !!ri.byKindAndId[joinKindAndId(k, id)];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function assertUuid(expected: ResourceName, actual: string | undefined) {
|
|
|
|
if (actual && !actual.startsWith(expected)) {
|
|
|
|
console.warn(`
|
2018-11-25 11:45:22 -07:00
|
|
|
UUID integrity warning! Application expected ${expected} type, but instead
|
|
|
|
received "${actual}"`);
|
2018-04-25 11:45:22 -06:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|