Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code point parsing #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/codepoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { ValidationError } = require('./errors')

function isNumber(value) {
return value.match(/^((0x[\da-f]+)|(\d+))$/gi)
}

// https://www.w3.org/International/questions/qa-escapes#cssescapes
function isUnicodeEscape(value) {
return value.match(/^\\[\da-f]{1,6}$/gi)
}

function parseCodePoint(codePoint, propName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible to consider parse value which ise recived from json outpu like '\f101' ?
Or maybe is possible to get json for icon-font-generator with js unicode like "\uf101' ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code to support CSS unicode escape codes. I think this should do what you ask for.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Hammie really thank You for that. This is exactly what i wont.

if(isNumber(codePoint)) {
return Number.parseInt(codePoint)
}
else if(codePoint.length === 1) {
return codePoint.charCodeAt(0)
}
else if(isUnicodeEscape(codePoint)) {
return Number.parseInt(codePoint.slice(1), 16)
}
else {
throw new ValidationError('Codepoints map contains invalid code point for icon \'' + propName + '\'')
}
}

module.exports = { parseCodePoint }
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
} = require('./constants')
const { ValidationError } = require('./errors')
const { log, logOutput } = require('./utils')
const { parseCodePoint } = require('./codepoint')
const fsAsync = {
exists : promisify(fs.exists),
stat : promisify(fs.stat),
Expand Down Expand Up @@ -157,7 +158,7 @@ async function getCodepointsMap(filepath) {
}

for (let propName in codepointsMap) {
codepointsMap[propName] = Number.parseInt(codepointsMap[propName])
codepointsMap[propName] = parseCodePoint(codepointsMap[propName], propName)
}

return codepointsMap
Expand Down