-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[helpers] split
helpers
into 2 new packages: user-agent-helpers
&…
… `client-hints-helpers`
- Loading branch information
Showing
16 changed files
with
543 additions
and
359 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
---|---|---|
@@ -1,57 +1,64 @@ | ||
#!/usr/bin/env node | ||
/* jshint esversion: 6 */ | ||
const fs = require('fs'); | ||
const PATH = { | ||
main : { | ||
src : 'src/main/ua-parser.js', | ||
dest : 'src/main/ua-parser.mjs', | ||
title : '' | ||
}, | ||
enums : { | ||
src : 'src/enums/ua-parser-enums.js', | ||
dest :'src/enums/ua-parser-enums.mjs', | ||
title : 'enums' | ||
}, | ||
extensions : { | ||
src : 'src/extensions/ua-parser-extensions.js', | ||
dest : 'src/extensions/ua-parser-extensions.mjs', | ||
title : 'extensions' | ||
}, | ||
helpers : { | ||
src : 'src/helpers/ua-parser-helpers.js', | ||
dest : 'src/helpers/ua-parser-helpers.mjs', | ||
title : 'helpers' | ||
} | ||
}; | ||
const generateMJS = (module, replacers) => { | ||
const { src, dest, title } = PATH[module]; | ||
|
||
const generateMJS = (module) => { | ||
let { src, dest, title, replacements } = module; | ||
let text = fs.readFileSync(src, 'utf-8'); | ||
replacers.forEach(replacer => { | ||
text = text.replace(replacer[0], replacer[1]); | ||
|
||
replacements.push( | ||
[/const (.+?)\s*=\s*require\((.+)\)/ig, 'import $1 from $2'], | ||
[/module\.exports =/ig, 'export'] | ||
); | ||
replacements.forEach(rep => { | ||
text = text.replace(rep[0], rep[1]); | ||
}); | ||
|
||
console.log(`Generate ${dest}`); | ||
|
||
fs.writeFileSync(dest, | ||
`// Generated ESM version of UAParser.js ${title} | ||
`// Generated ESM version of ${title} | ||
// DO NOT EDIT THIS FILE! | ||
// Source: /${src} | ||
${text}`, 'utf-8'); | ||
}; | ||
|
||
// ua-parser.mjs | ||
generateMJS('main', [ | ||
[/\(func[\s\S]+strict\';/ig, ''], | ||
[/esversion\: 3/ig, 'esversion: 6'], | ||
[/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};'] | ||
]); | ||
|
||
// ua-parser-enum.mjs | ||
generateMJS('enums', [[/module\.exports =/ig, 'export']]); | ||
}; | ||
|
||
// ua-parser-extension.mjs | ||
generateMJS('extensions', [[/module\.exports =/ig, 'export']]); | ||
const modules = [ | ||
{ | ||
src : 'src/main/ua-parser.js', | ||
dest : 'src/main/ua-parser.mjs', | ||
title : 'ua-parser-js', | ||
replacements : [ | ||
[/\(func[\s\S]+strict\';/ig, ''], | ||
[/esversion\: 3/ig, 'esversion: 6'], | ||
[/\/[\/\s]+export[\s\S]+/ig,'export {UAParser};'] | ||
] | ||
},{ | ||
src : 'src/enums/ua-parser-enums.js', | ||
dest :'src/enums/ua-parser-enums.mjs', | ||
title : 'ua-parser-js/enums', | ||
replacements : [] | ||
}, | ||
{ | ||
src : 'src/extensions/ua-parser-extensions.js', | ||
dest : 'src/extensions/ua-parser-extensions.mjs', | ||
title : 'ua-parser-js/extensions', | ||
replacements : [] | ||
}, | ||
{ | ||
src : 'src/user-agent-helpers/user-agent-helpers.js', | ||
dest : 'src/user-agent-helpers/user-agent-helpers.mjs', | ||
title : '@ua-parser-js/user-agent-helpers', | ||
replacements : [] | ||
}, | ||
{ | ||
src : 'src/client-hints-helpers/client-hints-helpers.js', | ||
dest : 'src/client-hints-helpers/client-hints-helpers.mjs', | ||
title : '@ua-parser-js/client-hints-helpers', | ||
replacements : [] | ||
} | ||
]; | ||
|
||
// ua-parser-helpers.mjs | ||
generateMJS('helpers', [[/module\.exports =/ig, 'export']]); | ||
modules.forEach(module => generateMJS(module)); |
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,88 @@ | ||
//////////////////////////////////////////////////// | ||
/* A collection of utility methods for client-hints | ||
https://github.com/faisalman/ua-parser-js | ||
Author: Faisal Salman <f@faisalman.com> | ||
MIT License */ | ||
/////////////////////////////////////////////////// | ||
|
||
/*jshint esversion: 11 */ | ||
|
||
const UACHMap = { | ||
'sec-ch-ua-arch' : { | ||
prop : 'architecture', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-bitness' : { | ||
prop : 'bitness', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua' : { | ||
prop : 'brands', | ||
type : 'sf-list' | ||
}, | ||
'sec-ch-ua-form-factor' : { | ||
prop : 'formFactor', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-full-version-list' : { | ||
prop : 'fullVersionList', | ||
type : 'sf-list' | ||
}, | ||
'sec-ch-ua-mobile' : { | ||
prop : 'mobile', | ||
type : 'sf-boolean', | ||
}, | ||
'sec-ch-ua-model' : { | ||
prop : 'model', | ||
type : 'sf-string', | ||
}, | ||
'sec-ch-ua-platform' : { | ||
prop : 'platform', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-platform-version' : { | ||
prop : 'platformVersion', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-wow64' : { | ||
prop : 'wow64', | ||
type : 'sf-boolean' | ||
} | ||
}; | ||
|
||
const UACHParser = (headers) => { | ||
const parse = (str, type) => { | ||
if (!str) { | ||
return ''; | ||
} | ||
switch (type) { | ||
case 'sf-boolean': | ||
return /\?1/.test(str); | ||
case 'sf-list': | ||
return str.replace(/\\?\"/g, '') | ||
.split(', ') | ||
.map(brands => { | ||
const [brand, version] = brands.split(';v='); | ||
return { | ||
brand : brand, | ||
version : version | ||
}; | ||
}); | ||
case 'sf-string': | ||
default: | ||
return str.replace(/\\?\"/g, ''); | ||
} | ||
}; | ||
let ch = {}; | ||
Object.keys(UACHMap).forEach(field => { | ||
if (headers.hasOwnProperty(field)) { | ||
const { prop, type } = UACHMap[field]; | ||
ch[prop] = parse(headers[field], type); | ||
} | ||
}); | ||
return ch; | ||
}; | ||
|
||
module.exports = { | ||
UACHParser | ||
}; |
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,92 @@ | ||
// Generated ESM version of @ua-parser-js/client-hints-helpers | ||
// DO NOT EDIT THIS FILE! | ||
// Source: /src/client-hints-helpers/client-hints-helpers.js | ||
|
||
//////////////////////////////////////////////////// | ||
/* A collection of utility methods for client-hints | ||
https://github.com/faisalman/ua-parser-js | ||
Author: Faisal Salman <f@faisalman.com> | ||
MIT License */ | ||
/////////////////////////////////////////////////// | ||
|
||
/*jshint esversion: 11 */ | ||
|
||
const UACHMap = { | ||
'sec-ch-ua-arch' : { | ||
prop : 'architecture', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-bitness' : { | ||
prop : 'bitness', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua' : { | ||
prop : 'brands', | ||
type : 'sf-list' | ||
}, | ||
'sec-ch-ua-form-factor' : { | ||
prop : 'formFactor', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-full-version-list' : { | ||
prop : 'fullVersionList', | ||
type : 'sf-list' | ||
}, | ||
'sec-ch-ua-mobile' : { | ||
prop : 'mobile', | ||
type : 'sf-boolean', | ||
}, | ||
'sec-ch-ua-model' : { | ||
prop : 'model', | ||
type : 'sf-string', | ||
}, | ||
'sec-ch-ua-platform' : { | ||
prop : 'platform', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-platform-version' : { | ||
prop : 'platformVersion', | ||
type : 'sf-string' | ||
}, | ||
'sec-ch-ua-wow64' : { | ||
prop : 'wow64', | ||
type : 'sf-boolean' | ||
} | ||
}; | ||
|
||
const UACHParser = (headers) => { | ||
const parse = (str, type) => { | ||
if (!str) { | ||
return ''; | ||
} | ||
switch (type) { | ||
case 'sf-boolean': | ||
return /\?1/.test(str); | ||
case 'sf-list': | ||
return str.replace(/\\?\"/g, '') | ||
.split(', ') | ||
.map(brands => { | ||
const [brand, version] = brands.split(';v='); | ||
return { | ||
brand : brand, | ||
version : version | ||
}; | ||
}); | ||
case 'sf-string': | ||
default: | ||
return str.replace(/\\?\"/g, ''); | ||
} | ||
}; | ||
let ch = {}; | ||
Object.keys(UACHMap).forEach(field => { | ||
if (headers.hasOwnProperty(field)) { | ||
const { prop, type } = UACHMap[field]; | ||
ch[prop] = parse(headers[field], type); | ||
} | ||
}); | ||
return ch; | ||
}; | ||
|
||
export { | ||
UACHParser | ||
}; |
14 changes: 7 additions & 7 deletions
14
src/helpers/package.json → src/client-hints-helpers/package.json
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
Oops, something went wrong.