Skip to content
Closed
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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ parsers.parseCompositionExclusions = require('./scripts/parse-composition-exclus
parsers.parseLineBreak = require('./scripts/parse-line-break.js');
parsers.parseScriptExtensions = require('./scripts/parse-script-extensions.js');
parsers.parseWordBreak = require('./scripts/parse-word-break.js');
parsers.parseName = require('./scripts/parse-name.js');
const extend = utils.extend;
const cp = require('cp');
const jsesc = require('jsesc');
Expand Down Expand Up @@ -118,6 +119,12 @@ const generateData = function(version) {
'map': parsers.parseWordBreak(version),
'type': 'Word_Break'
}));
console.log('Parsing Unicode v%s `Name`…', version);
extend(dirMap, utils.writeFiles({
'version': version,
'map': parsers.parseName(version),
'type': 'Name'
}));
// Sort array values.
Object.keys(dirMap).forEach(function(property) {
if (Array.isArray(dirMap[property])) {
Expand Down
51 changes: 51 additions & 0 deletions scripts/parse-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const utils = require('./utils.js');

const parseName = function(version) {
const map = {};
const source = utils.readDataFile(version, 'database');
if (!source) {
return;
}
const lines = source.split('\n');

let flag = false;
let first = 0;
lines.forEach(function(line) {
const data = line.trim().split(';');
const codePoint = parseInt(data[0], 16);
const name = data[1];

if (!isNaN(codePoint)) {
var match = /<([^>]+)>/.exec(name);

if (match) {
const rangeName = /(.+), (First|Last)/.exec(match[1]);

if (rangeName) {
if (flag && rangeName[2] === 'Last') {
flag = false;

utils.range(first, codePoint).forEach(function (value) {
utils.append(map, rangeName[1] + ' ' + utils.codePointToHex(value), value);
});
} else if (!flag && rangeName[2] === 'First') {
flag = true;
first = codePoint;
} else {
throw Error('Database exception');
}
} else {
utils.append(map, match[1] + ' ' + utils.codePointToHex(codePoint), codePoint);
}
} else {
utils.append(map, name, codePoint);
}
}
});

return map;
};

module.exports = parseName;
16 changes: 12 additions & 4 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const writeFiles = function(options) {
type == 'Bidi_Class' ||
type == 'Bidi_Mirroring_Glyph' ||
type == 'Bidi_Paired_Bracket_Type' ||
type == 'Name' ||
(
type == 'General_Category' &&
// Use the most specific category names, i.e. those whose aliases match
Expand All @@ -78,7 +79,7 @@ const writeFiles = function(options) {
auxMap[type][codePoint] = item;
});
}
if (type == 'Bidi_Mirroring_Glyph') {
if (type == 'Bidi_Mirroring_Glyph' || type == 'Name') {
return;
}
append(dirMap, type, item);
Expand Down Expand Up @@ -129,7 +130,7 @@ const writeFiles = function(options) {
}
mkdirp.sync(dir);
let output = '';
if (/^(?:Bidi_Class|Bidi_Mirroring_Glyph|bidi-brackets)$/.test(type)) {
if (/^(?:Bidi_Class|Bidi_Mirroring_Glyph|bidi-brackets|Name)$/.test(type)) {
const map = new Map();
Object.keys(auxMap[type]).forEach(function(key) {
const codePoint = Number(key);
Expand All @@ -139,7 +140,7 @@ const writeFiles = function(options) {
if ('Bidi_Mirroring_Glyph' == type) { // `Bidi_Mirroring_Glyph/index.js`
// Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip.
output = `module.exports=${ jsesc(map) }`;
} else { // `Bidi_Class/index.js` or `bidi-brackets/index.js`
} else { // `Bidi_Class/index.js` or `bidi-brackets/index.js` or `Name/index.js`
output = `module.exports=${ gzipInline(map) }`;
}
} else { // `categories/index.js`
Expand Down Expand Up @@ -179,10 +180,17 @@ const readDataFile = function(version, type) {
return source;
};

const codePointToHex = function (codePoint) {
var hexString = codePoint.toString(16).toUpperCase();

return ('0000' + hexString).slice(-Math.max(4, hexString.length));
};

module.exports = {
'range': range,
'append': append,
'extend': extend,
'readDataFile': readDataFile,
'writeFiles': writeFiles
'writeFiles': writeFiles,
'codePointToHex': codePointToHex
};