const fs = require ( 'fs' ) ; const mdn = require ( '@mdn/browser-compat-data' ) ; const path = require ( 'path' ) ; const filename = path . resolve ( `${ __dirname }/../src/browser-compat-data.ts` ) ; param { string } browserName param { bcd . SimpleSupportStatement } supportStatement returns { boolean } const isUniversalSupportStatement = ( browserName , supportStatement ) => { const version = supportStatement . version_added ; if ( supportStatement . flags || supportStatement . prefix ) { return false ; } if ( supportStatement . version_removed ) { return false ; } if ( typeof supportStatement . version_removed === 'string' ) { return false ; } if ( typeof version === 'boolean' ) { return version ; } if ( version === null ) { return true ; } if ( version === '1' || version === '1.0' ) { return true ; } switch ( browserName ) { case 'chrome' : case 'chrome_android' : case 'opera' : case 'opera_android' : return parseInt ( version ) <= 49 ; case 'edge' : case 'edge_mobile' : return version === '12' ; case 'firefox' : case 'firefox_android' : return parseInt ( version ) <= 49 ; case 'ie' : return parseInt ( version ) <= 9 ; case 'safari' : case 'safari_ios' : return parseInt ( version ) <= 9 ; case 'webview_android' : return parseInt ( version ) <= 4 ; default : return false ; } } ; param { string } browserName param { bcd . SupportBlock } support returns { bcd . SimpleSupportStatement [ ] } const getSupportStatements = ( browserName , support ) => { const browserData = support [ browserName ] || [ ] ; return Array . isArray ( browserData ) ? browserData : [ browserData ] ; } ; param { bcd . CompatStatement } compat returns { boolean } const isFeatureUniversallySupported = ( compat ) => { for ( const browserName of Object . keys ( compat . support ) ) { for ( const supportStatement of getSupportStatements ( browserName , compat . support ) ) { if ( ! isUniversalSupportStatement ( browserName , supportStatement ) ) { return false ; } } } return true ; } ; param { bcd . CompatStatement } compat const isFeatureUniversallyUnsupported = ( compat ) => { for ( const browserName of Object . keys ( compat . support ) ) { const supportStatements = getSupportStatements ( browserName , compat . support ) ; if ( supportStatements . length > 1 || supportStatements [ 0 ] . version_added ) { return false ; } } return true ; } ; param { string } browserName param { bcd . SupportBlock } support const removeFlaggedSupport = ( browserName , support ) => { const supportStatements = getSupportStatements ( browserName , support ) ; const unflagged = supportStatements . filter ( ( supportStatement ) => { return ! supportStatement . flags ; } ) ; if ( unflagged . length ) { support [ browserName ] = unflagged . length === 1 ? unflagged [ 0 ] : unflagged ; } else { support [ browserName ] = { version_added : false } ; } } ; param { string } browserName param { bcd . SupportBlock } support const removeSupportNotes = ( browserName , support ) => { for ( const supportStatement of getSupportStatements ( browserName , support ) ) { delete supportStatement . notes ; } } ; param { string } browserName param { bcd . SupportBlock } support const removeUniversalSupport = ( browserName , support ) => { for ( const supportStatement of getSupportStatements ( browserName , support ) ) { if ( isUniversalSupportStatement ( browserName , supportStatement ) ) { delete support [ browserName ] ; return ; } } } ; param { bcd . Identifier } data const removeFeatureData = ( data ) => { if ( ! data . __compat ) { return ; } const compat = data . __compat ; const support = data . __compat . support ; delete compat . description ; delete data . __compat . status ; for ( const browserName of Object . keys ( support ) ) { removeFlaggedSupport ( browserName , support ) ; removeUniversalSupport ( browserName , support ) ; removeSupportNotes ( browserName , support ) ; } if ( isFeatureUniversallySupported ( compat ) || isFeatureUniversallyUnsupported ( compat ) ) { if ( Object . keys ( data ) . length === 1 ) { delete data . __compat ; } else { delete data . __compat . support ; } } } ; param { bcd . Identifier } data const removeFeatures = ( data ) => { for ( const key of Object . keys ( data ) ) { if ( key === '__compat' ) { removeFeatureData ( data ) ; } else { removeFeatures ( data [ key ] ) ; if ( ! Object . keys ( data [ key ] ) . length ) { delete data [ key ] ; } } } } ; param { bcd . Browsers } browsers const removeBrowserDetails = ( browsers ) => { for ( const browserName of Object . keys ( browsers ) ) { browsers [ browserName ] = type { any } ( { name : browsers [ browserName ] . name } ) ; } } ; const data = { browsers : mdn . browsers , css : mdn . css , html : mdn . html } ; removeBrowserDetails ( data . browsers ) ; removeFeatures ( data . css ) ; removeFeatures ( data . html ) ; const code = `/* eslint-disable */
import { Browsers, PrimaryIdentifier } from '@mdn/browser-compat-data/types';

type Data = {
    browsers: Browsers;
    css: PrimaryIdentifier;
    html: PrimaryIdentifier;
}

export const mdn: Data = ${ JSON . stringify ( data , null , 4 ) } as any;
` ; fs . writeFile ( filename , code , ( err ) => { if ( err ) { throw err ; } else { console . log ( `Created: ${ filename }` ) ; } } ) ;