Skip to content

Commit

Permalink
refactor!: [emoji] was removed without replacements, please use cus…
Browse files Browse the repository at this point in the history
…tom function if you need this
  • Loading branch information
alexander-akait committed Oct 20, 2021
1 parent 93a87ce commit d19e92f
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 73 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ The following tokens are replaced in the `name` parameter:
- `[path]` the path of the resource relative to the `context` query parameter or option.
- `[folder]` the folder the resource is in
- `[query]` the queryof the resource, i.e. `?foo=bar`
- `[emoji]` a random emoji representation of `options.content`
- `[emoji:<length>]` same as above, but with a customizable number of emojis
- `[contenthash]` the hash of `options.content` (Buffer) (by default it's the hex digest of the md4 hash)
- `[<hashType>:contenthash:<digestType>:<length>]` optionally one can configure
- other `hashType`s, i. e. `sha1`, `md4`, `md5`, `sha256`, `sha512`
Expand Down Expand Up @@ -190,14 +188,6 @@ loaderUtils.interpolateName(loaderContext, "html-[hash:6].html", { content: ...
loaderUtils.interpolateName(loaderContext, "[hash]", { content: ... });
// => c31e9820c001c9c4a86bce33ce43b679

// loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif"
loaderUtils.interpolateName(loaderContext, "[emoji]", { content: ... });
// => 👍

// loaderContext.resourcePath = "/absolute/path/to/app/img/image.gif"
loaderUtils.interpolateName(loaderContext, "[emoji:4]", { content: ... });
// => 🙍🏢📤🐝

// loaderContext.resourcePath = "/absolute/path/to/app/img/image.png"
loaderUtils.interpolateName(loaderContext, "[sha512:hash:base64:7].[ext]", { content: ... });
// => 2BKDTjl.png
Expand Down
35 changes: 0 additions & 35 deletions lib/interpolateName.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
"use strict";

const path = require("path");
const emojisList = require("emojis-list");
const getHashDigest = require("./getHashDigest");

const emojiRegex = /[\uD800-\uDFFF]./;
const emojiList = emojisList.filter((emoji) => emojiRegex.test(emoji));
const emojiCache = {};

function encodeStringToEmoji(content, length) {
if (emojiCache[content]) {
return emojiCache[content];
}

length = length || 1;

const emojis = [];

do {
if (!emojiList.length) {
throw new Error("Ran out of emoji");
}

const index = Math.floor(Math.random() * emojiList.length);

emojis.push(emojiList[index]);
emojiList.splice(index, 1);
} while (--length > 0);

const emojiEncoding = emojis.join("");

emojiCache[content] = emojiEncoding;

return emojiEncoding;
}

function interpolateName(loaderContext, name, options) {
let filename;

Expand Down Expand Up @@ -111,9 +79,6 @@ function interpolateName(loaderContext, name, options) {
/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi,
(all, hashType, digestType, maxLength) =>
getHashDigest(content, hashType, digestType, parseInt(maxLength, 10))
)
.replace(/\[emoji(?::(\d+))?\]/gi, (all, length) =>
encodeStringToEmoji(content, parseInt(length, 10))
);
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"description": "utils for webpack loaders",
"dependencies": {
"big.js": "^6.1.1",
"emojis-list": "^3.0.0",
"json5": "^2.2.0"
},
"scripts": {
Expand Down
27 changes: 0 additions & 27 deletions test/interpolateName.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const loaderUtils = require("../");

const emojiRegex = /[\uD800-\uDFFF]./;

describe("interpolateName()", () => {
function run(tests) {
tests.forEach((test) => {
Expand Down Expand Up @@ -273,21 +271,6 @@ describe("interpolateName()", () => {
"[unrecognized]",
"should not interpolate unrecognized token",
],
[
[{}, "[emoji]", { content: "test" }],
(result) => {
expect(emojiRegex.test(result)).toBe(true);
},
"should interpolate [emoji]",
],
[
[{}, "[emoji:3]", { content: "string" }],
(result) => {
expect(emojiRegex.test(result)).toBe(true);
expect(result.length).toBeDefined();
},
"should interpolate [emoji:3]",
],
]);

it("should return the same emoji for the same string", () => {
Expand All @@ -298,16 +281,6 @@ describe("interpolateName()", () => {
expect(result1).toBe(result2);
});

it("should throw error when out of emoji", () => {
expect(() => {
loaderUtils.interpolateName.apply(loaderUtils, [
{},
"[emoji:5000]",
{ content: "foo" },
]);
}).toThrow("Ran out of emoji");
});

describe("no loader context", () => {
const loaderContext = {};
run([
Expand Down

0 comments on commit d19e92f

Please sign in to comment.