generated from MengLinMaker/npm-library-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add filterPreprocessCompatData function for external runtime use
- Loading branch information
1 parent
f3d70b2
commit 8121723
Showing
3 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import _preprocessCompatData from './preprocessCompatData.json' | ||
|
||
import type { RuntimeName } from 'runtime-compat-data' | ||
import { objectKeys } from './objectKeys' | ||
import type { | ||
PreprocessCompatData, | ||
PreprocessCompatStatement, | ||
RuntimeCompatData, | ||
RuntimeCompatStatement, | ||
} from './types.js' | ||
|
||
const preprocessCompatData: PreprocessCompatData = _preprocessCompatData | ||
export { preprocessCompatData } | ||
|
||
/** | ||
* Extract unsupported runtimes based of filter. | ||
* @param preprocessCompatStatement | ||
* @param filterRuntimes - Runtimes to filter for lack of support detection. | ||
* @returns Array of unsupported runtimes. | ||
*/ | ||
const getUnsupportedRuntimes = ( | ||
preprocessCompatStatement: PreprocessCompatStatement, | ||
filterRuntimes: RuntimeName[], | ||
) => { | ||
const unsupportedRuntimes: RuntimeName[] = [] | ||
|
||
for (const filterRuntime of filterRuntimes) { | ||
const support = preprocessCompatStatement.support[filterRuntime] | ||
if (support === undefined) { | ||
// Runtime not found, therefore unsupported. | ||
unsupportedRuntimes.push(filterRuntime) | ||
} else if (Array.isArray(support)) { | ||
// Array format not supported by runtime-compat-data, therefore unsupported. | ||
unsupportedRuntimes.push(filterRuntime) | ||
} else if (support.version_added === false) { | ||
// Only boolean is supported in runtime-compat-data. | ||
unsupportedRuntimes.push(filterRuntime) | ||
} | ||
} | ||
return unsupportedRuntimes | ||
} | ||
|
||
/** | ||
* Clean flat compat data object, retaining only unsupported runtimes. | ||
* @param flatCompatData - Flat compat data object. | ||
* @param filterRuntimes - Runtimes to filter for lack of support detection. | ||
* @returns Parsed unsupported runtime data. | ||
*/ | ||
export const filterPreprocessCompatData = ( | ||
preprocessCompatData: PreprocessCompatData, | ||
filterRuntimes: RuntimeName[], | ||
) => { | ||
const parsedCompatData: RuntimeCompatData = { | ||
class: new Map<string, RuntimeCompatStatement>(), | ||
classProperty: new Map<string, RuntimeCompatStatement>(), | ||
eventListener: new Map<string, RuntimeCompatStatement>(), | ||
global: new Map<string, RuntimeCompatStatement>(), | ||
globalClassProperty: new Map<string, RuntimeCompatStatement>(), | ||
misc: new Map<string, RuntimeCompatStatement>(), | ||
} | ||
for (const context of objectKeys(preprocessCompatData)) { | ||
for (const jsonKeys of objectKeys(preprocessCompatData[context])) { | ||
const preprocessCompatStatement = preprocessCompatData[context][jsonKeys] | ||
if (preprocessCompatStatement) { | ||
const unsupportedRuntimes = getUnsupportedRuntimes( | ||
preprocessCompatStatement, | ||
filterRuntimes, | ||
) | ||
if (unsupportedRuntimes.length > 0) { | ||
parsedCompatData[context].set(jsonKeys, { | ||
url: preprocessCompatStatement.url, | ||
status: preprocessCompatStatement.status, | ||
unsupported: unsupportedRuntimes, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
return parsedCompatData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { RuntimeName } from 'runtime-compat-data' | ||
import { describe, expect, it } from 'vitest' | ||
import { objectKeys } from '../objectKeys' | ||
import { filterPreprocessCompatData, preprocessCompatData } from '../runtime' | ||
|
||
describe('filterPreprocessCompatData', () => { | ||
const filterRuntimes: RuntimeName[] = ['node'] | ||
|
||
it('should successfully filter for ', () => { | ||
const runtimeCompatData = filterPreprocessCompatData(preprocessCompatData, filterRuntimes) | ||
for (const context of objectKeys(runtimeCompatData)) { | ||
for (const [jsonKeys, runtimeCompatStatement] of runtimeCompatData[context].entries()) { | ||
const keys = JSON.parse(jsonKeys) as string[] | ||
expect(keys.length).toBeGreaterThan(0) | ||
expect(runtimeCompatStatement.unsupported).toStrictEqual(filterRuntimes) | ||
} | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters