Skip to content

Commit

Permalink
Refactor module
Browse files Browse the repository at this point in the history
Refactor `readme.md`; update dependencies, dev-dependencies;
remove unused dot-files; simpler linting.
  • Loading branch information
wooorm committed Jun 28, 2016
1 parent ad98ff3 commit 936db06
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 385 deletions.
8 changes: 1 addition & 7 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,mdastrc,eslintrc,sh}]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
6 changes: 0 additions & 6 deletions .eslintignore

This file was deleted.

6 changes: 0 additions & 6 deletions .eslintrc

This file was deleted.

3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
.DS_Store
*.log
build/
components/
coverage/
node_modules/
build.js
stringify-entities.js
stringify-entities.min.js
38 changes: 0 additions & 38 deletions .jscs.json

This file was deleted.

1 change: 0 additions & 1 deletion .mdastignore

This file was deleted.

13 changes: 0 additions & 13 deletions .mdastrc

This file was deleted.

6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
language: node_js
node_js:
- '0.10'
- '0.11'
- '0.12'
- '4.0'
- '5.0'
- iojs
after_script: npm install codecov.io && cat ./coverage/lcov.info | codecov
- '6.0'
after_success: bash <(curl -s https://codecov.io/bash)
deploy:
provider: releases
api_key:
Expand All @@ -16,3 +15,4 @@ deploy:
- "stringify-entities.min.js"
on:
tags: true
node: '6.0'
24 changes: 0 additions & 24 deletions component.json

This file was deleted.

6 changes: 3 additions & 3 deletions history.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!--mdast setext-->
<!--remark setext-->

<!--lint disable no-multiple-toplevel-headings -->
<!--lint disable no-multiple-toplevel-headings-->

1.0.1 / 2015-12-27
==================

* Fix typo in `readme.md` ([fca0bc9](https://github.com/wooorm/stringify-entities/commit/fca0bc9))
* Fix typo in `component.json` ([fe7ecec](https://github.com/wooorm/stringify-entities/commit/fe7ecec))
* Fix typographic styling in readme.md` ([b5c09db](https://github.com/wooorm/stringify-entities/commit/b5c09db))
* Fix typographic styling in readme.md\` ([b5c09db](https://github.com/wooorm/stringify-entities/commit/b5c09db))

1.0.0 / 2015-11-22
==================
114 changes: 48 additions & 66 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,28 @@

/* eslint-env commonjs */

/*
* Dependencies.
*/

/* Dependencies. */
var entities = require('character-entities-html4');
var EXPRESSION_NAMED = require('./lib/expression.js');

/*
* Methods.
*/

/* Methods. */
var has = {}.hasOwnProperty;

/*
* List of enforced escapes.
*/

/* List of enforced escapes. */
var escapes = ['"', '\'', '<', '>', '&', '`'];

/*
* Map of characters to names.
*/

/* Map of characters to names. */
var characters = {};

(function () {
var name;
var name;

for (name in entities) {
characters[entities[name]] = name;
}
for (name in entities) {
characters[entities[name]] = name;
}
})();

/*
* Regular expressions.
*/

/* Regular expressions. */
var EXPRESSION_ESCAPE = toExpression(escapes);
var EXPRESSION_SURROGATE_PAIR = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
var EXPRESSION_BMP = /[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g;
Expand All @@ -58,7 +43,7 @@ var EXPRESSION_BMP = /[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF
* @return {string} - `code` encoded as hexadecimal.
*/
function characterCodeToHexadecimalReference(code) {
return '&#x' + code.toString(16).toUpperCase() + ';';
return '&#x' + code.toString(16).toUpperCase() + ';';
}

/**
Expand All @@ -69,7 +54,7 @@ function characterCodeToHexadecimalReference(code) {
* @return {string} - `character` encoded as hexadecimal.
*/
function characterToHexadecimalReference(character) {
return characterCodeToHexadecimalReference(character.charCodeAt(0));
return characterCodeToHexadecimalReference(character.charCodeAt(0));
}

/**
Expand All @@ -79,7 +64,7 @@ function characterToHexadecimalReference(character) {
* @return {string} - `name` encoded as hexadecimal.
*/
function toNamedEntity(name) {
return '&' + name + ';';
return '&' + name + ';';
}

/**
Expand All @@ -89,7 +74,7 @@ function toNamedEntity(name) {
* @return {string} - `name` encoded as hexadecimal.
*/
function characterToNamedEntity(character) {
return toNamedEntity(characters[character]);
return toNamedEntity(characters[character]);
}

/**
Expand All @@ -99,7 +84,7 @@ function characterToNamedEntity(character) {
* @return {RegExp} - Expression.
*/
function toExpression(characters) {
return new RegExp('[' + characters.join('') + ']', 'g');
return new RegExp('[' + characters.join('') + ']', 'g');
}

/**
Expand All @@ -116,35 +101,35 @@ function toExpression(characters) {
* @return {string} - Encoded `value`.
*/
function encode(value, options) {
var settings = options || {};
var escapeOnly = settings.escapeOnly;
var named = settings.useNamedReferences;
var subset = settings.subset;
var map = named ? characters : null;
var set = subset ? toExpression(subset) : EXPRESSION_ESCAPE;

value = value.replace(set, function (character) {
return map && has.call(map, character) ?
toNamedEntity(map[character]) :
characterToHexadecimalReference(character);
});

if (subset || escapeOnly) {
return value;
}

if (named) {
value = value.replace(EXPRESSION_NAMED, characterToNamedEntity);
}

return value
.replace(EXPRESSION_SURROGATE_PAIR, function (pair) {
return characterCodeToHexadecimalReference(
(pair.charCodeAt(0) - 0xD800) * 0x400 +
pair.charCodeAt(1) - 0xDC00 + 0x10000
);
})
.replace(EXPRESSION_BMP, characterToHexadecimalReference);
var settings = options || {};
var escapeOnly = settings.escapeOnly;
var named = settings.useNamedReferences;
var subset = settings.subset;
var map = named ? characters : null;
var set = subset ? toExpression(subset) : EXPRESSION_ESCAPE;

value = value.replace(set, function (character) {
return map && has.call(map, character) ?
toNamedEntity(map[character]) :
characterToHexadecimalReference(character);
});

if (subset || escapeOnly) {
return value;
}

if (named) {
value = value.replace(EXPRESSION_NAMED, characterToNamedEntity);
}

return value
.replace(EXPRESSION_SURROGATE_PAIR, function (pair) {
return characterCodeToHexadecimalReference(
((pair.charCodeAt(0) - 0xD800) * 0x400) +
pair.charCodeAt(1) - 0xDC00 + 0x10000
);
})
.replace(EXPRESSION_BMP, characterToHexadecimalReference);
}

/**
Expand All @@ -154,16 +139,13 @@ function encode(value, options) {
* @return {string} - Encoded `value`.
*/
function escape(value) {
return encode(value, {
'escapeOnly': true,
'useNamedReferences': true
});
return encode(value, {
escapeOnly: true,
useNamedReferences: true
});
}

encode.escape = escape;

/*
* Expose.
*/

/* Expose. */
module.exports = encode;
Loading

0 comments on commit 936db06

Please sign in to comment.