-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from nikolai-shabalin/feature/values
Add values
- Loading branch information
Showing
6 changed files
with
83 additions
and
125 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
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 |
---|---|---|
@@ -1,128 +1,76 @@ | ||
import bcd from '@mdn/browser-compat-data' with { type: 'json' }; | ||
import fs from 'node:fs'; | ||
|
||
const {css, browsers } = bcd; | ||
let data = []; | ||
|
||
const getSpecUrl = (properties, propertyName) => { | ||
const specUrl = properties[propertyName]['__compat']['spec_url']; | ||
return Array.isArray(specUrl) ? specUrl[0] : specUrl; | ||
} | ||
|
||
const getBrowserVersion = (browser) => { | ||
if (browser) { | ||
let version = Array.isArray(browser.version_added) ? browser.version_added[0] : browser.version_added; | ||
if (version === 'preview') { | ||
return null; | ||
} | ||
if (typeof version === 'string' && version.startsWith('≤')) { | ||
version = parseFloat(version.substring(1)); | ||
} | ||
return version; | ||
} | ||
return null; | ||
} | ||
const { css, browsers } = bcd; | ||
let groupedData = {}; | ||
|
||
const getMinDate = (browserVersionsSupportingProperty) => { | ||
const releaseDates = []; | ||
|
||
for (let browserVersion of browserVersionsSupportingProperty) { | ||
const { name, version } = browserVersion; | ||
const releaseInfo = browsers[name]['releases'][version]; | ||
if (!releaseInfo || !releaseInfo['release_date']) { | ||
continue; | ||
for (let { name, version } of browserVersionsSupportingProperty) { | ||
const releaseInfo = browsers[name]?.['releases'][version]; | ||
if (releaseInfo?.release_date) { | ||
releaseDates.push(releaseInfo.release_date); | ||
} | ||
const releaseDate = releaseInfo['release_date']; | ||
releaseDates.push(releaseDate); | ||
} | ||
|
||
if (releaseDates.length === 0) { | ||
return false; | ||
} | ||
|
||
const minDate = releaseDates.reduce((minDate, current) => { | ||
const currentDate = new Date(current); | ||
return currentDate < minDate ? currentDate : minDate; | ||
}, new Date(releaseDates[0])); | ||
return minDate.toISOString().split('T')[0]; | ||
} | ||
|
||
const getDate = ({status: {deprecated}, support: {chrome, firefox, safari}}, name) => { | ||
if (deprecated) { | ||
return false; | ||
} | ||
|
||
const chromeVersion = getBrowserVersion(chrome); | ||
const firefoxVersion = getBrowserVersion(firefox); | ||
const safariVersion = getBrowserVersion(safari); | ||
|
||
if (!chromeVersion && !firefoxVersion && !safariVersion) { | ||
return false; | ||
} | ||
|
||
const browserVersions = [ | ||
chromeVersion && {name: 'chrome', version: chromeVersion}, | ||
firefoxVersion && {name: 'firefox', version: firefoxVersion}, | ||
safariVersion && {name: 'safari', version: safariVersion} | ||
].filter(Boolean); | ||
|
||
const minDate = getMinDate(browserVersions, name); | ||
|
||
if (minDate === false) { | ||
return false; | ||
}; | ||
|
||
const getBrowserSupport = (compat) => { | ||
const supportedBrowsers = []; | ||
for (let [browser, supportData] of Object.entries(compat.support || {})) { | ||
if (typeof supportData === "string" || !supportData) continue; | ||
const versionAdded = Array.isArray(supportData.version_added) | ||
? supportData.version_added[0] | ||
: supportData.version_added; | ||
if (versionAdded) { | ||
supportedBrowsers.push({ name: browser, version: versionAdded }); | ||
} | ||
} | ||
return supportedBrowsers; | ||
}; | ||
|
||
return minDate; | ||
} | ||
|
||
const renameType = (type) => ({ | ||
'at-rules': '@', | ||
'properties': 'Свойство', | ||
'selectors': 'Селектор', | ||
'types': 'Тип' | ||
}[type] || type); | ||
const extractData = (properties, parent = null) => { | ||
for (let [propertyName, propertyData] of Object.entries(properties)) { | ||
const compat = propertyData.__compat; | ||
if (!compat) continue; | ||
|
||
for (const cssKey in css) { | ||
const type = renameType(cssKey); | ||
const properties = css[cssKey]; | ||
const browserSupport = getBrowserSupport(compat); | ||
const date = getMinDate(browserSupport); | ||
if (!date) continue; | ||
|
||
for (const propertyName in properties) { | ||
const name = propertyName; | ||
const mdnUrl = properties[propertyName]['__compat']['mdn_url']; | ||
const specUrl = getSpecUrl(properties, propertyName); | ||
|
||
const date = getDate(properties[propertyName]['__compat'], name); | ||
|
||
if (date === false) continue; | ||
|
||
data.push({ | ||
name, | ||
mdnUrl, | ||
specUrl, | ||
const year = date.split("-")[0]; | ||
const entry = { | ||
name: propertyName, | ||
date, | ||
type, | ||
type: parent ? "Значение" : "Свойство", | ||
parent: parent || null, | ||
mdnUrl: compat.mdn_url || null, | ||
specUrl: compat.spec_url || null, | ||
year: date.split('-')[0] | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
const sortDataByYear = (data) => { | ||
const sortedData = {}; | ||
for (let item of data) { | ||
const year = item.date.split('-')[0]; | ||
if (!sortedData[year]) { | ||
sortedData[year] = []; | ||
if (!groupedData[year]) { | ||
groupedData[year] = []; | ||
} | ||
sortedData[year].push(item); | ||
} | ||
groupedData[year].push(entry); | ||
|
||
for (let year in sortedData) { | ||
sortedData[year].sort((a, b) => b.date.localeCompare(a.date)); | ||
extractData(propertyData, propertyName); | ||
} | ||
}; | ||
|
||
return sortedData; | ||
for (const cssKey in css) { | ||
extractData(css[cssKey]); | ||
} | ||
|
||
const sortedData = sortDataByYear(data); | ||
for (const year in groupedData) { | ||
groupedData[year].sort((a, b) => b.date.localeCompare(a.date)); | ||
} | ||
|
||
fs.writeFileSync('./src/data/data.js', `export const data = ${JSON.stringify(sortedData)}`); | ||
fs.writeFileSync('./data.js', `export const data = ${JSON.stringify(groupedData)};`); |
Large diffs are not rendered by default.
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