forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalkingUtils.ts
50 lines (42 loc) · 1.39 KB
/
walkingUtils.ts
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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import { Identifier, BrowserStatement } from '../types/types.js';
/**
* Join a path array together
* @param args The path to join together
* @returns The combined path
*/
export const joinPath = (...args: (string | undefined)[]): string =>
Array.from(args).filter(Boolean).join('.');
/**
* Check if an object is a BCD feature
* @param obj The object to check
* @returns Whether the object is a BCD feature
*/
export const isFeature = (obj): obj is Identifier => '__compat' in obj;
/**
* Check if an object is a browser statement
* @param obj The object to check
* @returns Whether the object is a browser statement
*/
export const isBrowser = (obj): obj is BrowserStatement =>
'name' in obj && 'releases' in obj;
/**
* Get the descendant keys of an object, minus any features that start with two underscores
* @param data The object to iterate
* @returns The descendant keys
*/
export const descendantKeys = (data) => {
if (!data || typeof data !== 'object') {
// Return if the data isn't an object
return [];
}
if (isFeature(data)) {
return Object.keys(data).filter((key) => !key.startsWith('__'));
}
if (isBrowser(data)) {
// Browsers never have independently meaningful descendants
return [];
}
return Object.keys(data).filter((key) => key !== '__meta');
};