email-all-chars-within-ascii3.0.14

Scans all characters within a string and checks are they within ASCII range

§ Quick Take

import { strict as assert } from "assert";
import { within } from "email-all-chars-within-ascii";

// enforces all characters to be within ASCII:
assert.deepEqual(within(`<div>Motörhead</div>`), [
  {
    type: "character",
    line: 1,
    column: 9,
    positionIdx: 8,
    value: "ö",
    codePoint: 246,
    UTF32Hex: "00f6",
  },
]);

// enforces line lengths (500 is best for email):
assert.deepEqual(within(`abcde`, { lineLength: 3 }), [
  {
    type: "line length",
    line: 1,
    column: 5,
    positionIdx: 5,
    value: 5,
  },
]);

§ Idea

Traverse the string and check if all characters are suitable for 7bit encoding, in other words, are within the basic ASCII range, first 126 characters, and does not include any invisible control characters.

We don't want any invisible control characters (anything below decimal point 32), EXCEPT:

  • HT, horizontal tab, decimal number 9
  • LF, new line, decimal number 10
  • CR, carriage return, decimal number 13

Often decimal point 127, DEL, is overlooked, yet it is not right in your templates, especially email.

In that sense, non-ascii regex opens in a new tab and the likes are dangerous to validate your email template code because they are too lax.

Also, we want an error reported if any lines exceed the recommended line length in email, 500 characters-per-line.

§ The API

within(str, [opts])

In other words, it's a function, second input argument is optional (marked by brackets).

Input:

  • the first argument - string only or program will throw.
  • the second argument - a plain object. Anything else, if truthy, will throw.

§ API - Options:

options object's keyTypeObligatory?DefaultDescription
lineLengthnumberno500Allowed max line length allowed. Set to 0 to disable.

Here are all defaults in one place:

{
lineLength: 500,
}

§ Changelog

See it in the monorepo opens in a new tab, on GitHub.

§ Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 email-all-chars-within-ascii-cli 1.12.14
Command line app to scan email templates, are all their characters within ASCII range
📦 edit-package-json 0.3.14
Edit package.json without parsing, as string, to keep the formatting intact
📦 easy-replace 4.0.14
Replace strings with optional lookarounds, but without regexes
📦 str-indexes-of-plus 3.0.14
Like indexOf but returns array and counts per-grapheme
📦 js-row-num 4.0.14
Update all row numbers in all console.logs in JS code
📦 line-column-mini 1.1.14
Convert string index to line-column position
📦 email-homey 2.9.2
Generate homepage in the Browsersync root with links/screenshots to all your email templates