Skip to content

Commit

Permalink
feat: return UTF32Hex value too
Browse files Browse the repository at this point in the history
  • Loading branch information
revelt committed Jan 19, 2021
1 parent ff28b11 commit 6db600a
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions packages/email-all-chars-within-ascii/README.md
Expand Up @@ -45,6 +45,7 @@ assert.deepEqual(within(`<div>Motörhead</div>`), [
positionIdx: 8,
value: "ö",
codePoint: 246,
UTF32Hex: "00f6",
},
]);

Expand Down
@@ -1 +1 @@
{"total":{"lines":{"total":25,"covered":25,"skipped":0,"pct":100},"statements":{"total":27,"covered":27,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":34,"covered":34,"skipped":0,"pct":100}}}
{"total":{"lines":{"total":26,"covered":26,"skipped":0,"pct":100},"statements":{"total":28,"covered":28,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"branches":{"total":34,"covered":34,"skipped":0,"pct":100}}}
Expand Up @@ -19,6 +19,7 @@ var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);

var version = "2.10.1";

var version$1 = version;
var defaults = {
lineLength: 500
};
Expand Down Expand Up @@ -100,7 +101,8 @@ function within(str, originalOpts) {
column: column,
positionIdx: i,
value: str[i],
codePoint: currCodePoint
codePoint: currCodePoint,
UTF32Hex: str[i].charCodeAt(0).toString(16).padStart(4, "0").toLowerCase()
});
}
} //
Expand All @@ -112,5 +114,5 @@ function within(str, originalOpts) {
}

exports.defaults = defaults;
exports.version = version;
exports.version = version$1;
exports.within = within;
Expand Up @@ -64,6 +64,7 @@ function _objectSpread2(target) {

var version = "2.10.1";

var version$1 = version;
var defaults = {
lineLength: 500
};
Expand Down Expand Up @@ -145,7 +146,8 @@ function within(str, originalOpts) {
column: column,
positionIdx: i,
value: str[i],
codePoint: currCodePoint
codePoint: currCodePoint,
UTF32Hex: str[i].charCodeAt(0).toString(16).padStart(4, "0").toLowerCase()
});
}
} //
Expand All @@ -157,7 +159,7 @@ function within(str, originalOpts) {
}

exports.defaults = defaults;
exports.version = version;
exports.version = version$1;
exports.within = within;

Object.defineProperty(exports, '__esModule', { value: true });
Expand Down
Expand Up @@ -9,6 +9,7 @@

var version = "2.10.1";

const version$1 = version;
const defaults = {
lineLength: 500
};
Expand Down Expand Up @@ -91,7 +92,8 @@ function within(str, originalOpts) {
column,
positionIdx: i,
value: str[i],
codePoint: currCodePoint
codePoint: currCodePoint,
UTF32Hex: str[i].charCodeAt(0).toString(16).padStart(4, "0").toLowerCase()
});
}
} //
Expand All @@ -102,4 +104,4 @@ function within(str, originalOpts) {
return res;
}

export { defaults, version, within };
export { defaults, version$1 as version, within };
Expand Up @@ -7,4 +7,4 @@
* Homepage: https://codsen.com/os/email-all-chars-within-ascii/
*/

var t="2.10.1";const n={lineLength:500};function i(t,i){if("string"!=typeof t)throw new Error(`email-all-chars-within-ascii/within(): [THROW_ID_01] The input is not string but ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(i&&"object"!=typeof i)throw new Error(`email-all-chars-within-ascii/within(): [THROW_ID_02] The opts is not a plain object but ${typeof i}, equal to:\n${JSON.stringify(i,null,4)}`);if(!t.length)return[];const e={...n,...i};let o=0,l=1;const r=[];for(let n=0,i=t.length;n<=i;n++)if(e.lineLength&&(!t[n]||"\r"===t[n]||"\n"===t[n])&&o>e.lineLength&&r.push({type:"line length",line:l,column:o,positionIdx:n,value:o}),"\r"===t[n]||"\n"===t[n]?(o=0,"\n"!==t[n]&&"\n"===t[n+1]||(l+=1)):o+=1,t[n]){const i=t[n].codePointAt(0);(void 0===i||i>126||i<9||11===i||12===i||i>13&&i<32)&&r.push({type:"character",line:l,column:o,positionIdx:n,value:t[n],codePoint:i})}return r}export{n as defaults,t as version,i as within};
const t="2.10.1",n={lineLength:500};function i(t,i){if("string"!=typeof t)throw new Error(`email-all-chars-within-ascii/within(): [THROW_ID_01] The input is not string but ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(i&&"object"!=typeof i)throw new Error(`email-all-chars-within-ascii/within(): [THROW_ID_02] The opts is not a plain object but ${typeof i}, equal to:\n${JSON.stringify(i,null,4)}`);if(!t.length)return[];const e={...n,...i};let o=0,r=1;const l=[];for(let n=0,i=t.length;n<=i;n++)if(e.lineLength&&(!t[n]||"\r"===t[n]||"\n"===t[n])&&o>e.lineLength&&l.push({type:"line length",line:r,column:o,positionIdx:n,value:o}),"\r"===t[n]||"\n"===t[n]?(o=0,"\n"!==t[n]&&"\n"===t[n+1]||(r+=1)):o+=1,t[n]){const i=t[n].codePointAt(0);(void 0===i||i>126||i<9||11===i||12===i||i>13&&i<32)&&l.push({type:"character",line:r,column:o,positionIdx:n,value:t[n],codePoint:i,UTF32Hex:t[n].charCodeAt(0).toString(16).padStart(4,"0").toLowerCase()})}return l}export{n as defaults,t as version,i as within};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -12,6 +12,7 @@ assert.deepEqual(within(`<div>Motörhead</div>`), [
positionIdx: 8,
value: "ö",
codePoint: 246,
UTF32Hex: "00f6",
},
]);

Expand Down
2 changes: 1 addition & 1 deletion packages/email-all-chars-within-ascii/examples/api.json
@@ -1 +1 @@
{"_quickTake.js":{"title":"Quick Take","content":"import &#x7B; strict as assert &#x7D; from \"assert\";\nimport &#x7B; within &#x7D; from \"email-all-chars-within-ascii\";\n\n// enforces all characters to be within ASCII:\nassert.deepEqual(within(`<div>Motörhead</div>`), [\n &#x7B;\n type: \"character\",\n line: 1,\n column: 9,\n positionIdx: 8,\n value: \"ö\",\n codePoint: 246,\n &#x7D;,\n]);\n\n// enforces line lengths (500 is best for email):\nassert.deepEqual(within(`abcde`, &#x7B; lineLength: 3 &#x7D;), [\n &#x7B;\n type: \"line length\",\n line: 1,\n column: 5,\n positionIdx: 5,\n value: 5,\n &#x7D;,\n]);"}}
{"_quickTake.js":{"title":"Quick Take","content":"import &#x7B; strict as assert &#x7D; from \"assert\";\nimport &#x7B; within &#x7D; from \"email-all-chars-within-ascii\";\n\n// enforces all characters to be within ASCII:\nassert.deepEqual(within(`<div>Motörhead</div>`), [\n &#x7B;\n type: \"character\",\n line: 1,\n column: 9,\n positionIdx: 8,\n value: \"ö\",\n codePoint: 246,\n UTF32Hex: \"00f6\",\n &#x7D;,\n]);\n\n// enforces line lengths (500 is best for email):\nassert.deepEqual(within(`abcde`, &#x7B; lineLength: 3 &#x7D;), [\n &#x7B;\n type: \"line length\",\n line: 1,\n column: 5,\n positionIdx: 5,\n value: 5,\n &#x7D;,\n]);"}}
7 changes: 4 additions & 3 deletions packages/email-all-chars-within-ascii/package.json
Expand Up @@ -26,11 +26,11 @@
"browser": "dist/email-all-chars-within-ascii.umd.js",
"types": "types/main.d.ts",
"scripts": {
"build": "npm run clean_types -- --pt1 && rollup -c && npm run clean_types -- --pt2",
"build": "rollup -c",
"ci_test": "npm run build && npm run lint && tap --no-only --reporter=silent --output-file=testStats.md && npm run clean_cov && npm run format",
"clean_cov": "../../scripts/leaveCoverageTotalOnly.js",
"clean_types": "../../scripts/cleanTypes.js",
"dev": "npm run clean_types -- --pt1 && rollup -c --dev && npm run clean_types -- --pt2",
"dev": "rollup -c --dev",
"devunittest": "npm run dev && ./node_modules/.bin/tap --only -R 'base' && npm run tsd && npm run clean_cov",
"format": "npm run lect && npm run prettier && npm run lint",
"lect": "lect",
Expand All @@ -45,7 +45,7 @@
"test:examples": "../../scripts/test-examples.js && npm run lect && npm run prettier",
"tsc": "tsc",
"tsd": "test ! -f index.d.ts || tsd",
"unittest": "./node_modules/.bin/tap --no-only --output-file=testStats.md --reporter=terse && tsc --noEmit && npm run tsd && npm run clean_cov && npm run perf",
"unittest": "./node_modules/.bin/tap --no-only --output-file=testStats.md --reporter=terse && tsc -p tsconfig.json --noEmit && npm run tsd && npm run clean_cov && npm run perf",
"version": "npm run build && git add ."
},
"tap": {
Expand Down Expand Up @@ -104,6 +104,7 @@
"rollup-plugin-ascii": "^0.0.3",
"rollup-plugin-banner": "^0.2.1",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-dts": "^2.0.1",
"rollup-plugin-terser": "^7.0.2",
"tap": "^14.11.0",
"tsd": "^0.14.0",
Expand Down
11 changes: 9 additions & 2 deletions packages/email-all-chars-within-ascii/rollup.config.js
Expand Up @@ -8,6 +8,7 @@ import banner from "rollup-plugin-banner";
import babel from "@rollup/plugin-babel";
import strip from "@rollup/plugin-strip";
import json from "@rollup/plugin-json";
import dts from "rollup-plugin-dts";
import pkg from "./package.json";

const licensePiece = `${pkg.name}
Expand Down Expand Up @@ -134,8 +135,7 @@ export default (commandLineArgs) => {
json(),
typescript({
tsconfig: "../../tsconfig.build.json",
declaration: true,
declarationDir: "./types",
declaration: false,
}),
babel({
extensions,
Expand Down Expand Up @@ -240,6 +240,13 @@ export default (commandLineArgs) => {
banner(licensePiece),
],
},

// Type definitions
{
input: "src/main.ts",
output: [{ file: "types/main.d.ts", format: "es" }],
plugins: [json(), dts()],
},
];

if (commandLineArgs.dev) {
Expand Down
11 changes: 9 additions & 2 deletions packages/email-all-chars-within-ascii/src/main.ts
@@ -1,4 +1,5 @@
import { version } from "../package.json";
import { version as v } from "../package.json";
const version: string = v;

interface Res {
type: "character" | "line length";
Expand All @@ -7,6 +8,7 @@ interface Res {
positionIdx: number;
value: number | string;
codePoint?: undefined | number;
UTF32Hex?: undefined | string;
}

interface Opts {
Expand All @@ -17,7 +19,7 @@ const defaults: Opts = {
lineLength: 500,
};

function within(str: string, originalOpts?: Opts): Res[] {
function within(str: string, originalOpts?: Partial<Opts>): Res[] {
if (typeof str !== "string") {
throw new Error(
`email-all-chars-within-ascii/within(): [THROW_ID_01] The input is not string but ${typeof str}, equal to: ${JSON.stringify(
Expand Down Expand Up @@ -135,6 +137,11 @@ function within(str: string, originalOpts?: Opts): Res[] {
positionIdx: i,
value: str[i],
codePoint: currCodePoint,
UTF32Hex: str[i]
.charCodeAt(0)
.toString(16)
.padStart(4, "0")
.toLowerCase(),
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/email-all-chars-within-ascii/test/test.js
Expand Up @@ -30,6 +30,7 @@ tap.test("04 - illegal", (t) => {
positionIdx: 3,
value: "ö",
codePoint: 246,
UTF32Hex: "00f6",
},
]);
t.end();
Expand All @@ -48,6 +49,7 @@ tap.test("05 - multi-line illegal", (t) => {
positionIdx: 10,
value: CAPITALOWITHSTROKE,
codePoint: 216,
UTF32Hex: "00d8",
},
{
type: "character",
Expand All @@ -56,6 +58,7 @@ tap.test("05 - multi-line illegal", (t) => {
positionIdx: 25,
value: SMALLOWITHDIAERESIS,
codePoint: 246,
UTF32Hex: "00f6",
},
]
);
Expand All @@ -71,6 +74,7 @@ tap.test("06 - illegal low ASCII, ETX", (t) => {
positionIdx: 1,
value: ETX,
codePoint: 3,
UTF32Hex: "0003",
},
]);
t.end();
Expand All @@ -86,6 +90,7 @@ tap.test("07 - non-existent outside ASCII, #896 or u0380", (t) => {
positionIdx: 1,
value: NONEXISTING,
codePoint: 896,
UTF32Hex: "0380",
},
]);
t.end();
Expand Down
8 changes: 5 additions & 3 deletions packages/email-all-chars-within-ascii/types/main.d.ts
@@ -1,15 +1,17 @@
import { version } from "../package.json";
declare const version: string;
interface Res {
type: "character" | "line length";
line: number;
column: number;
positionIdx: number;
value: number | string;
codePoint?: undefined | number;
UTF32Hex?: undefined | string;
}
interface Opts {
lineLength: number;
}
declare const defaults: Opts;
declare function within(str: string, originalOpts?: Opts): Res[];
export { within, defaults, version };
declare function within(str: string, originalOpts?: Partial<Opts>): Res[];

export { defaults, version, within };

0 comments on commit 6db600a

Please sign in to comment.