Skip to content

Commit

Permalink
Add getEsmaVersionFromUA function
Browse files Browse the repository at this point in the history
  • Loading branch information
ije committed Aug 22, 2024
1 parent 71d9f2a commit e162cda
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@ npm i esm-compat
## Usage

```js
import { getBuildTargetFromUA } from 'esm-compat';
import { getEsmaVersionFromUA } from "esm-compat"

const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
const target = getBuildTargetFromUA(ua)
console.log(target) // => es2022
```

## API

```ts
export const targets: Set<string>;
export const getBuildTargetFromUA: (ua: string | null) => string;
const version = getEsmaVersionFromUA(ua)
console.log(version) // => es2022
```

## License
Expand Down
29 changes: 19 additions & 10 deletions src/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ const jsTable: Record<string, Record<string, Version>> = {
Chrome: [91, 0, 0],
Deno: [1, 17, 0],
Edge: [91, 0, 0],
Node: [2, 0, 0],
Node: [22, 0, 0],
},
ImportAttributes: {
Chrome: [123, 0, 0],
Expand Down Expand Up @@ -482,14 +482,14 @@ const jsTable: Record<string, Record<string, Version>> = {
Opera: [33, 0, 0],
Safari: [10, 0, 0],
},
// NodeColonPrefixImport: {
// ES: [0, 0, 0],
// Node: [14, 13, 1],
// },
// NodeColonPrefixRequire: {
// ES: [0, 0, 0],
// Node: [16, 0, 0],
// },
NodeColonPrefixImport: {
ES: [0, 0, 0],
Node: [14, 13, 1],
},
NodeColonPrefixRequire: {
ES: [0, 0, 0],
Node: [16, 0, 0],
},
NullishCoalescing: {
// Note: The latest version of "IE" failed this test: nullish coalescing operator (??)
// Note: The latest version of "Rhino" failed this test: nullish coalescing operator (??)
Expand Down Expand Up @@ -780,7 +780,7 @@ const esmaUnsupportedFeatures: [string, number][] = [

const rVersion = /^(\d+)\.(\d+)\.(\d+)/;

/** get build target from the `User-Agent` header by checking the `jsTable` object. */
/** get the build target for esm.sh service by checking the given `user-agent` header. */
export const getBuildTargetFromUA = (userAgent: string | null) => {
if (!userAgent || userAgent.startsWith("curl/")) {
return "esnext";
Expand Down Expand Up @@ -825,3 +825,12 @@ export const getBuildTargetFromUA = (userAgent: string | null) => {
}
return "es2015";
};

/** get the esma version of a browser by checking the given `user-agent` header. */
export const getEsmaVersionFromUA = (userAgent: string | null) => {
const target = getBuildTargetFromUA(userAgent);
if (target.startsWith("es")) {
return target;
}
return "esnext";
};
17 changes: 7 additions & 10 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { getBuildTargetFromUA } from "./dist/compat.js";

const testData = {
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36":
"es2023",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15":
"es2021",
"ES/2022": "es2022",
"Deno/1.33.1": "deno",
"Deno/1.33.2": "denonext",
"Node.js/20.0.0": "node",
"undici": "node",

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36": "es2023",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15": "es2021",
"ES/2022": "es2022",
"Deno/1.33.1": "deno",
"Deno/1.33.2": "denonext",
"Node.js/20.0.0": "node",
"undici": "node",
};

for (const [ua, expected] of Object.entries(testData)) {
Expand Down
1 change: 1 addition & 0 deletions types/compat.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const targets: Set<string>;
export const getBuildTargetFromUA: (ua: string | null) => string;
export const getEsmaVersionFromUA: (ua: string | null) => string;

0 comments on commit e162cda

Please sign in to comment.