summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/util/getIndexKeys.ts
blob: 416cf9ea2a7dbe4831be9c9c7cf8278d869b36e5 (plain)
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
import { Key, Value, KeyPath } from "./types";
import extractKey from "./extractKey";
import valueToKey from "./valueToKey";

export function getIndexKeys(
  value: Value,
  keyPath: KeyPath,
  multiEntry: boolean,
): Key[] {
  if (multiEntry && Array.isArray(keyPath)) {
    const keys = [];
    for (const subkeyPath of keyPath) {
      const key = extractKey(subkeyPath, value);
      try {
        const k = valueToKey(key);
        keys.push(k);
      } catch {
        // Ignore invalid subkeys
      }
    }
    return keys;
  } else {
    let key = extractKey(keyPath, value);
    return [valueToKey(key)];
  }
}

export default getIndexKeys;