postcss-rename
Version:
A PostCSS plugin to replace class names based on a customizable renaming scheme.
81 lines • 3.01 kB
JavaScript
/**
* @license
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinimalRenamer = exports.toShortName = void 0;
/**
* Possible first chars in a CSS name. This only includes ASCII characters to
* avoid the risk of encoding mismatches, and it doesn't include `-` in case the
* user is doing by-part renaming.
*/
const START_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_'.split('');
/**
* Possible non-initial chars in a CSS name. This only includes ASCII characters
* to avoid the risk of encoding mismatches, and it doesn't include `-` in case
* the user is doing by-part renaming.
*/
const CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'.split('');
/**
* Returns the next unique short string whose first character is in
* `START_CHARS` and whose subsequent characters, if any, are in `CHARS`.
*
* This is public so it can be unit-tested.
*/
function toShortName(index) {
const result = [START_CHARS[index % START_CHARS.length]];
if (index < START_CHARS.length)
return result.join('');
index = Math.floor(index / START_CHARS.length) - 1;
// eslint-disable-next-line no-constant-condition
while (true) {
result.push(CHARS[index % CHARS.length]);
if (index < CHARS.length)
break;
index = Math.floor(index / CHARS.length) - 1;
}
return result.join('');
}
exports.toShortName = toShortName;
/** Renames CSS names to the smallest valid identifiers. */
class MinimalRenamer {
/**
* Creates a new MinimalSubstitutionMap that generates CSS names from the
* specified set of characters.
*
* @param skip A function which decides if given CSS names may not be
* returned as the output from a substitution lookup.
*/
constructor(skip) {
this.skip = skip;
/** The next index to pass to `toShortName()`. */
this.nextIndex = 0;
/** A map from original CSS names to their renamed equivalents. */
this.renames = new Map();
}
rename(key) {
let value = this.renames.get(key);
if (value)
return value;
do {
value = toShortName(this.nextIndex++);
} while (this.skip(value));
this.renames.set(key, value);
return value;
}
}
exports.MinimalRenamer = MinimalRenamer;
//# sourceMappingURL=minimal-renamer.js.map
;