Skip to content

Commit

Permalink
Converted code to ESNext (#37).
Browse files Browse the repository at this point in the history
  • Loading branch information
dmester committed Jul 30, 2020
1 parent dc3a068 commit 44401ac
Show file tree
Hide file tree
Showing 25 changed files with 644 additions and 686 deletions.
74 changes: 36 additions & 38 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,34 @@
*
* This file contains the public interface of Jdenticon for browsers.
*/
"use strict";

import { iconGenerator } from "./renderer/iconGenerator";
import { SvgRenderer } from "./renderer/svg/svgRenderer";
import { SvgElement } from "./renderer/svg/svgElement";
import { SvgWriter } from "./renderer/svg/svgWriter";
import { renderDomElement } from "./renderer/renderDomElement";
import { computeHash, isValidHash } from "./common/hashUtils";
import { CanvasRenderer } from "./renderer/canvas/index";
import { configuration } from "./common/configuration";
import { observer } from "./common/observer";
import { ICON_SELECTOR, ICON_TYPE_CANVAS, ICON_TYPE_SVG, getIdenticonType, supportsQuerySelectorAll } from "./common/dom";

const pack = require("../package.json");
const iconGenerator = require("./renderer/iconGenerator");
const SvgRenderer = require("./renderer/svg/svgRenderer");
const SvgElement = require("./renderer/svg/svgElement");
const SvgWriter = require("./renderer/svg/svgWriter");
const renderDomElement = require("./renderer/renderDomElement");
const hashUtils = require("./common/hashUtils");
const CanvasRenderer = require("./renderer/canvas");
const configuration = require("./common/configuration");
const observer = require("./common/observer");
const dom = require("./common/dom");


// <debug>
var global = typeof window !== "undefined" ? window : {},
jQuery = global.jQuery;
// </debug>

/**
* Updates all canvas elements with the `data-jdenticon-hash` or `data-jdenticon-value` attribute.
*/
function jdenticon() {
if (supportsQuerySelectorAll) {
update(ICON_SELECTOR);
}
}

/**
* Updates the identicon in the specified `<canvas>` or `<svg>` elements.
* @param {(string|Element)} el - Specifies the container in which the icon is rendered as a DOM element of the type
Expand All @@ -35,10 +44,10 @@ var global = typeof window !== "undefined" ? window : {},
* specified in place of a configuration object.
*/
function update(el, hashOrValue, config) {
renderDomElement(el, hashOrValue, config, function (el) {
var iconType = dom.getIdenticonType(el);
renderDomElement(el, hashOrValue, configuration(jdenticon, global, config, 0.08), function (el) {
const iconType = getIdenticonType(el);
if (iconType) {
return iconType == dom.ICON_TYPE_SVG ?
return iconType == ICON_TYPE_SVG ?
new SvgRenderer(new SvgElement(el)) :
new CanvasRenderer(el.getContext("2d"));
}
Expand All @@ -56,9 +65,9 @@ function update(el, hashOrValue, config) {
* specified in place of a configuration object.
*/
function updateCanvas(el, hashOrValue, config) {
renderDomElement(el, hashOrValue, config, function (el) {
var iconType = dom.getIdenticonType(el);
if (iconType == dom.ICON_TYPE_CANVAS) {
renderDomElement(el, hashOrValue, configuration(jdenticon, global, config, 0.08), function (el) {
const iconType = getIdenticonType(el);
if (iconType == ICON_TYPE_CANVAS) {
return new CanvasRenderer(el.getContext("2d"));
}
});
Expand All @@ -75,9 +84,9 @@ function updateCanvas(el, hashOrValue, config) {
* specified in place of a configuration object.
*/
function updateSvg(el, hashOrValue, config) {
renderDomElement(el, hashOrValue, config, function (el) {
var iconType = dom.getIdenticonType(el);
if (iconType == dom.ICON_TYPE_SVG) {
renderDomElement(el, hashOrValue, configuration(jdenticon, global, config, 0.08), function (el) {
const iconType = getIdenticonType(el);
if (iconType == ICON_TYPE_SVG) {
return new SvgRenderer(new SvgElement(el));
}
});
Expand All @@ -97,9 +106,8 @@ function drawIcon(ctx, hashOrValue, size, config) {
throw new Error("No canvas specified.");
}

var renderer = new CanvasRenderer(ctx, size);
iconGenerator(renderer,
hashUtils.validHash(hashOrValue) || hashUtils.computeHash(hashOrValue),
iconGenerator(new CanvasRenderer(ctx, size),
isValidHash(hashOrValue) || computeHash(hashOrValue),
0, 0, size, configuration(jdenticon, global, config, 0));
}

Expand All @@ -113,28 +121,18 @@ function drawIcon(ctx, hashOrValue, size, config) {
* @returns {string} SVG string
*/
function toSvg(hashOrValue, size, config) {
var writer = new SvgWriter(size);
var renderer = new SvgRenderer(writer);
iconGenerator(renderer,
hashUtils.validHash(hashOrValue) || hashUtils.computeHash(hashOrValue),
const writer = new SvgWriter(size);
iconGenerator(new SvgRenderer(writer),
isValidHash(hashOrValue) || computeHash(hashOrValue),
0, 0, size, configuration(jdenticon, global, config, 0.08));
return writer.toString();
}

/**
* Updates all canvas elements with the `data-jdenticon-hash` or `data-jdenticon-value` attribute.
*/
function jdenticon() {
if (dom.supportsQuerySelectorAll) {
update(dom.ICON_SELECTOR);
}
}

/**
* This function is called once upon page load.
*/
function jdenticonStartup() {
var replaceMode = (jdenticon["config"] || global["jdenticon_config"] || { })["replaceMode"];
const replaceMode = (jdenticon["config"] || global["jdenticon_config"] || { })["replaceMode"];
if (replaceMode != "never") {
jdenticon();

Expand Down
16 changes: 7 additions & 9 deletions src/common/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* https://github.com/dmester/jdenticon
* Copyright © Daniel Mester Pirttijärvi
*/
"use strict";

const color = require("../renderer/color");
import { parseColor } from "../renderer/color";

/**
* Gets the normalized current Jdenticon color configuration. Missing fields have default values.
Expand All @@ -19,8 +18,8 @@ const color = require("../renderer/color");
* @param {number} defaultPadding - Padding used if no padding is specified in neither the configuration nor
* explicitly to the API method.
*/
function configuration(jdenticon, global, paddingOrLocalConfig, defaultPadding) {
var configObject =
export function configuration(jdenticon, global, paddingOrLocalConfig, defaultPadding) {
const configObject =
typeof paddingOrLocalConfig == "object" && paddingOrLocalConfig ||
jdenticon["config"] ||
global["jdenticon_config"] ||
Expand All @@ -41,7 +40,7 @@ function configuration(jdenticon, global, paddingOrLocalConfig, defaultPadding)
* Creates a lightness range.
*/
function lightness(configName, defaultRange) {
var range = lightnessConfig[configName];
let range = lightnessConfig[configName];

// Check if the lightness range is an array-like object. This way we ensure the
// array contain two values at the same time.
Expand All @@ -63,7 +62,8 @@ function configuration(jdenticon, global, paddingOrLocalConfig, defaultPadding)
* provided the originally computed hue.
*/
function hueFunction(originalHue) {
var hueConfig = configObject["hues"], hue;
const hueConfig = configObject["hues"];
let hue;

// Check if 'hues' is an array-like object. This way we also ensure that
// the array is not empty, which would mean no hue restriction.
Expand All @@ -90,12 +90,10 @@ function configuration(jdenticon, global, paddingOrLocalConfig, defaultPadding)
grayscaleSaturation: typeof grayscaleSaturation == "number" ? grayscaleSaturation : 0,
colorLightness: lightness("color", [0.4, 0.8]),
grayscaleLightness: lightness("grayscale", [0.3, 0.9]),
backColor: color.parse(backColor),
backColor: parseColor(backColor),
padding:
typeof paddingOrLocalConfig == "number" ? paddingOrLocalConfig :
typeof padding == "number" ? padding :
defaultPadding
}
}

module.exports = configuration;
34 changes: 11 additions & 23 deletions src/common/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,29 @@
* https://github.com/dmester/jdenticon
* Copyright © Daniel Mester Pirttijärvi
*/
"use strict";

var dom = {
/** @const */
ICON_TYPE_SVG: 1,
export const ICON_TYPE_SVG = 1;

/** @const */
ICON_TYPE_CANVAS: 2,

/** @const */
HASH_ATTRIBUTE: "data-jdenticon-hash",

/** @const */
VALUE_ATTRIBUTE: "data-jdenticon-value",
export const ICON_TYPE_CANVAS = 2;

supportsQuerySelectorAll: typeof document !== "undefined" && "querySelectorAll" in document,
export const HASH_ATTRIBUTE = "data-jdenticon-hash";

getIdenticonType: dom_getIdenticonType
};
export const VALUE_ATTRIBUTE = "data-jdenticon-value";

/** @const */
dom.ICON_SELECTOR = "[" + dom.HASH_ATTRIBUTE +"],[" + dom.VALUE_ATTRIBUTE +"]";
export const ICON_SELECTOR = "[" + HASH_ATTRIBUTE +"],[" + VALUE_ATTRIBUTE +"]";

function dom_getIdenticonType(el) {
export const supportsQuerySelectorAll = typeof document !== "undefined" && "querySelectorAll" in document;

export function getIdenticonType(el) {
if (el) {
var tagName = el["tagName"];
const tagName = el["tagName"];

if (/svg/i.test(tagName)) {
return dom.ICON_TYPE_SVG;
return ICON_TYPE_SVG;
}

if (/canvas/i.test(tagName) && "getContext" in el) {
return dom.ICON_TYPE_CANVAS;
return ICON_TYPE_CANVAS;
}
}
}

module.exports = dom;
35 changes: 15 additions & 20 deletions src/common/hashUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@
* https://github.com/dmester/jdenticon
* Copyright © Daniel Mester Pirttijärvi
*/
"use strict";

const sha1 = require("./sha1");
import { sha1 } from "./sha1";

var hashUtils = {
/**
* Inputs a value that might be a valid hash string for Jdenticon and returns it
* if it is determined valid, otherwise a falsy value is returned.
*/
validHash: function (hashCandidate) {
return /^[0-9a-f]{11,}$/i.test(hashCandidate) && hashCandidate;
},

/**
* Computes a hash for the specified value. Currnently SHA1 is used. This function
* always returns a valid hash.
*/
computeHash: function (value) {
return sha1(value == null ? "" : "" + value);
}
};
/**
* Inputs a value that might be a valid hash string for Jdenticon and returns it
* if it is determined valid, otherwise a falsy value is returned.
*/
export function isValidHash(hashCandidate) {
return /^[0-9a-f]{11,}$/i.test(hashCandidate) && hashCandidate;
}

module.exports = hashUtils;
/**
* Computes a hash for the specified value. Currnently SHA1 is used. This function
* always returns a valid hash.
*/
export function computeHash(value) {
return sha1(value == null ? "" : "" + value);
}
37 changes: 17 additions & 20 deletions src/common/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,45 @@
* https://github.com/dmester/jdenticon
* Copyright © Daniel Mester Pirttijärvi
*/
"use strict";

const dom = require("./dom");
import { HASH_ATTRIBUTE, ICON_SELECTOR, VALUE_ATTRIBUTE, getIdenticonType } from "./dom";

function observer(updateCallback) {
export function observer(updateCallback) {
if (typeof MutationObserver != "undefined") {
var mutationObserver = new MutationObserver(function onmutation(mutations) {
for (var mutationIndex = 0; mutationIndex < mutations.length; mutationIndex++) {
var mutation = mutations[mutationIndex];
var addedNodes = mutation.addedNodes;
const mutationObserver = new MutationObserver(function onmutation(mutations) {
for (let mutationIndex = 0; mutationIndex < mutations.length; mutationIndex++) {
const mutation = mutations[mutationIndex];
const addedNodes = mutation.addedNodes;

for (var addedNodeIndex = 0; addedNodes && addedNodeIndex < addedNodes.length; addedNodeIndex++) {
var addedNode = addedNodes[addedNodeIndex];
for (let addedNodeIndex = 0; addedNodes && addedNodeIndex < addedNodes.length; addedNodeIndex++) {
const addedNode = addedNodes[addedNodeIndex];

// Skip other types of nodes than element nodes, since they might not support
// the querySelectorAll method => runtime error.
if (addedNode.nodeType == Node.ELEMENT_NODE) {
if (dom.getIdenticonType(addedNode)) {
if (getIdenticonType(addedNode)) {
updateCallback(addedNode);
}
else {
var icons = addedNode.querySelectorAll(dom.ICON_SELECTOR);
for (var iconIndex = 0; iconIndex < icons.length; iconIndex++) {
const icons = addedNode.querySelectorAll(ICON_SELECTOR);
for (let iconIndex = 0; iconIndex < icons.length; iconIndex++) {
updateCallback(icons[iconIndex]);
}
}
}
}

if (mutation.type == "attributes" && dom.getIdenticonType(mutation.target)) {
if (mutation.type == "attributes" && getIdenticonType(mutation.target)) {
updateCallback(mutation.target);
}
}
});

mutationObserver.observe(document.body, {
"childList": true,
"attributes": true,
"attributeFilter": [dom.VALUE_ATTRIBUTE, dom.HASH_ATTRIBUTE, "width", "height"],
"subtree": true
mutationObserver.observe(document.body, {
"childList": true,
"attributes": true,
"attributeFilter": [VALUE_ATTRIBUTE, HASH_ATTRIBUTE, "width", "height"],
"subtree": true,
});
}
}

module.exports = observer;
5 changes: 1 addition & 4 deletions src/common/parseHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
* https://github.com/dmester/jdenticon
* Copyright © Daniel Mester Pirttijärvi
*/
"use strict";

/**
* Parses a substring of the hash as a number.
* @param {number} startPosition
* @param {number=} octets
* @noinline
*/
function parseHex(hash, startPosition, octets) {
export function parseHex(hash, startPosition, octets) {
return parseInt(hash.substr(startPosition, octets), 16);
}

module.exports = parseHex;
Loading

0 comments on commit 44401ac

Please sign in to comment.