add two chars to numberToIdentifier
This commit is contained in:
parent
1d3e1b4208
commit
6ef9ea8f27
|
@ -27,6 +27,9 @@ const { compareIds } = require("./util/comparators");
|
|||
const START_LOWERCASE_ALPHABET_CODE = "a".charCodeAt(0);
|
||||
const START_UPPERCASE_ALPHABET_CODE = "A".charCodeAt(0);
|
||||
const DELTA_A_TO_Z = "z".charCodeAt(0) - START_LOWERCASE_ALPHABET_CODE + 1;
|
||||
const NUMBER_OF_IDENTIFIER_START_CHARS = DELTA_A_TO_Z * 2 + 2; // a-z A-Z _ $
|
||||
const NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS =
|
||||
NUMBER_OF_IDENTIFIER_START_CHARS + 10; // a-z A-Z _ $ 0-9
|
||||
const FUNCTION_CONTENT_REGEX = /^function\s?\(\)\s?\{\r?\n?|\r?\n?\}$/g;
|
||||
const INDENT_MULTILINE_REGEX = /^\t/gm;
|
||||
const LINE_SEPARATOR_REGEX = /\r?\n/g;
|
||||
|
@ -124,30 +127,73 @@ class Template {
|
|||
.replace(MATCH_PADDED_HYPHENS_REPLACE_REGEX, "");
|
||||
}
|
||||
|
||||
// map number to a single character a-z, A-Z or <_ + number> if number is too big
|
||||
// map number to a single character a-z, A-Z or mulitple characters if number is too big
|
||||
/**
|
||||
*
|
||||
* @param {number} n number to convert to ident
|
||||
* @returns {string} returns single character ident
|
||||
*/
|
||||
static numberToIdentifier(n) {
|
||||
if (n >= NUMBER_OF_IDENTIFIER_START_CHARS) {
|
||||
// use multiple letters
|
||||
return (
|
||||
Template.numberToIdentifier(n % NUMBER_OF_IDENTIFIER_START_CHARS) +
|
||||
Template.numberToIdentifierContinuation(
|
||||
Math.floor(n / NUMBER_OF_IDENTIFIER_START_CHARS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// lower case
|
||||
if (n < DELTA_A_TO_Z) {
|
||||
return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
|
||||
}
|
||||
n -= DELTA_A_TO_Z;
|
||||
|
||||
// upper case
|
||||
if (n < DELTA_A_TO_Z * 2) {
|
||||
return String.fromCharCode(
|
||||
START_UPPERCASE_ALPHABET_CODE + n - DELTA_A_TO_Z
|
||||
if (n < DELTA_A_TO_Z) {
|
||||
return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
|
||||
}
|
||||
|
||||
if (n === DELTA_A_TO_Z) return "_";
|
||||
return "$";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} n number to convert to ident
|
||||
* @returns {string} returns single character ident
|
||||
*/
|
||||
static numberToIdentifierContinuation(n) {
|
||||
if (n >= NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS) {
|
||||
// use multiple letters
|
||||
return (
|
||||
Template.numberToIdentifierContinuation(
|
||||
n % NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
||||
) +
|
||||
Template.numberToIdentifierContinuation(
|
||||
Math.floor(n / NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// use multiple letters
|
||||
return (
|
||||
Template.numberToIdentifier(n % (2 * DELTA_A_TO_Z)) +
|
||||
Template.numberToIdentifier(Math.floor(n / (2 * DELTA_A_TO_Z)))
|
||||
);
|
||||
// lower case
|
||||
if (n < DELTA_A_TO_Z) {
|
||||
return String.fromCharCode(START_LOWERCASE_ALPHABET_CODE + n);
|
||||
}
|
||||
n -= DELTA_A_TO_Z;
|
||||
|
||||
// upper case
|
||||
if (n < DELTA_A_TO_Z) {
|
||||
return String.fromCharCode(START_UPPERCASE_ALPHABET_CODE + n);
|
||||
}
|
||||
n -= DELTA_A_TO_Z;
|
||||
|
||||
// numbers
|
||||
if (n < 10) {
|
||||
return `${n}`;
|
||||
}
|
||||
|
||||
if (n === 10) return "_";
|
||||
return "$";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -358,3 +404,5 @@ class Template {
|
|||
}
|
||||
|
||||
module.exports = Template;
|
||||
module.exports.NUMBER_OF_IDENTIFIER_START_CHARS = NUMBER_OF_IDENTIFIER_START_CHARS;
|
||||
module.exports.NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS = NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS;
|
||||
|
|
|
@ -6,7 +6,11 @@
|
|||
"use strict";
|
||||
|
||||
const { UsageState } = require("../ModuleGraph");
|
||||
const { numberToIdentifier } = require("../Template");
|
||||
const {
|
||||
numberToIdentifier,
|
||||
NUMBER_OF_IDENTIFIER_START_CHARS,
|
||||
NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
||||
} = require("../Template");
|
||||
const { assignDeterministicIds } = require("../ids/IdHelpers");
|
||||
const {
|
||||
concatComparators,
|
||||
|
@ -93,8 +97,11 @@ const mangleExportsInfo = (exportsInfo, canBeArray) => {
|
|||
e.usedName = name;
|
||||
return true;
|
||||
},
|
||||
[26, 52],
|
||||
52,
|
||||
[
|
||||
NUMBER_OF_IDENTIFIER_START_CHARS,
|
||||
NUMBER_OF_IDENTIFIER_START_CHARS * NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS
|
||||
],
|
||||
NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
|
||||
usedNames.size
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue