-
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.
cleanup, replace caniuse with caniuselite
- Loading branch information
1 parent
41f19b2
commit c0fe248
Showing
9 changed files
with
217 additions
and
156 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
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,66 @@ | ||
import data, { type AgentLiteKey, type MajorVersionStr, type UsageFloat } from './caniuse_lite.ts' | ||
import { UserAgent } from '$std/http/user_agent.ts' | ||
import { epochToDate } from './utils.ts' | ||
|
||
const agents = data.agents | ||
|
||
const BROWSER_NAME_AGENT_KEYS: Record<string, AgentLiteKey> = { | ||
'Chrome': 'chrome', | ||
'Mobile Firefox': 'and_ff', | ||
'Mobile Chrome': 'and_chr', | ||
'DuckDuckGo': 'chrome', | ||
'Edge': 'edge', | ||
'Firefox': 'firefox', | ||
'IE': 'ie', | ||
'Opera': 'opera', | ||
'Opera Mini': 'op_mini', | ||
'QQ Browser': 'and_qq', | ||
'Safari': 'safari', | ||
'Mobile Safari': 'ios_saf', | ||
'Samsung Internet': 'samsung', | ||
'UC': 'and_uc', | ||
} | ||
|
||
export type AgentRelease = { | ||
name: string | ||
version: MajorVersionStr | ||
userAgent?: UserAgent | null | ||
releaseDate: Date | ||
usage: UsageFloat | ||
currentVersion: MajorVersionStr | ||
} | ||
|
||
const findAgentStats = (name: string) => agents[BROWSER_NAME_AGENT_KEYS[name]] | ||
|
||
export const getAgentReleaseInfo = (ua: string): AgentRelease => { | ||
const userAgent = new UserAgent(ua) | ||
|
||
const name = userAgent.browser?.name | ||
const version = userAgent.browser?.major as MajorVersionStr | ||
|
||
if (!name || !version) { | ||
return { | ||
name: 'Unknown', | ||
userAgent, | ||
version: '0', | ||
releaseDate: new Date(0), | ||
usage: 0, | ||
currentVersion: '0', | ||
} | ||
} | ||
|
||
const agentStats = findAgentStats(name) | ||
|
||
const [lastReleaseVersion] = Object.entries(agentStats.release_date) | ||
.filter(([_, date]) => date !== null) | ||
.toSorted(([versionA], [versionB]) => parseFloat(versionB) - parseFloat(versionA))[0] | ||
|
||
return { | ||
name, | ||
version, | ||
userAgent, | ||
releaseDate: epochToDate(agentStats.release_date[version]), | ||
usage: agentStats.usage_global[version], | ||
currentVersion: lastReleaseVersion, | ||
} | ||
} |
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
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
import { agents } from 'caniuse-lite' | ||
|
||
export type EpochDate = number | ||
export type MajorVersionStr = string | ||
export type UsageFloat = number | ||
|
||
export type AgentLiteKey = | ||
| 'and_chr' | ||
| 'and_ff' | ||
| 'and_qq' | ||
| 'and_uc' | ||
| 'android' | ||
| 'baidu' | ||
| 'bb' | ||
| 'chrome' | ||
| 'edge' | ||
| 'firefox' | ||
| 'ie_mob' | ||
| 'ie' | ||
| 'ios_saf' | ||
| 'kaios' | ||
| 'op_mini' | ||
| 'op_mob' | ||
| 'opera' | ||
| 'safari' | ||
| 'samsung' | ||
|
||
export type AgentLiteStats = { | ||
usage_global: Record<MajorVersionStr, UsageFloat> | ||
prefix: string | ||
versions: MajorVersionStr[] | ||
browser: string | ||
release_date: Record<MajorVersionStr, EpochDate> | ||
} | ||
|
||
export type CanIUseLite = { | ||
agents: Record<AgentLiteKey, AgentLiteStats> | ||
} | ||
|
||
const data: CanIUseLite = { agents } | ||
|
||
export default data |
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,23 @@ | ||
import * as semver from '@std/semver' | ||
import type { Range as SemVerRange, SemVer } from '@std/semver' | ||
|
||
import { countLiterals } from './utils.ts' | ||
|
||
export const fuzzyParseSemVer = (version: string): SemVer | SemVerRange => { | ||
if (semver.tryParse(version)) return semver.parse(version) | ||
|
||
// If it looks like a range, then let's try to parse both sides loosely | ||
if (countLiterals(version, '-') === 1) { | ||
const minmax = version.split('-').map(fuzzyParseSemVer) | ||
if (minmax.every(semver.isSemVer)) { | ||
return semver.parseRange(minmax.map(semver.format).join('-')) | ||
} | ||
} | ||
|
||
version = version.replace(/\.+$/, '') | ||
while (!semver.tryParse(version) && countLiterals(version, '.') <= 2) { | ||
version += '.0' | ||
} | ||
|
||
return semver.parse(version) | ||
} |
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,18 @@ | ||
import { assertEquals } from 'jsr:@std/assert' | ||
import { parse, parseRange } from '@std/semver' | ||
import { fuzzyParseSemVer } from './semver.ts' | ||
|
||
Deno.test('fuzzyParseSemVer converts partial version strings to full semver', () => { | ||
assertEquals(fuzzyParseSemVer('5'), parse('5.0.0')) | ||
assertEquals(fuzzyParseSemVer('5.'), parse('5.0.0')) | ||
|
||
assertEquals(fuzzyParseSemVer('5.5'), parse('5.5.0')) | ||
assertEquals(fuzzyParseSemVer('5.5.'), parse('5.5.0')) | ||
|
||
assertEquals(fuzzyParseSemVer('5.0.0-alpha'), parse('5.0.0-alpha')) | ||
}) | ||
|
||
Deno.test('fuzzyParseSemVer converts partial ranges', () => { | ||
assertEquals(fuzzyParseSemVer('5.0-5.0'), parseRange('5.0.0-5.0.0')) | ||
assertEquals(fuzzyParseSemVer('8.1-8.4'), parseRange('8.1.0-8.4.0')) | ||
}) |
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,5 @@ | ||
// Convert an epoch timestamp to a Date | ||
export const epochToDate = (epoch: number): Date => new Date(epoch * 1000) | ||
|
||
// Count the number of occurrences of a character in a string | ||
export const countLiterals = (str: string, char: string): number => str.split(char).length - 1 |
Oops, something went wrong.