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,


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.


within(str, [opts])

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


  • 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 key Type Obligatory? Default Description
lineLength number no 500 Allowed max line length allowed. Set to 0 to disable.

Here are all defaults in one place:

lineLength: 500,


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


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.


MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 email-all-chars-within-ascii-cli 2.0.1
Command line app to scan email templates, are all their characters within ASCII range
📦 edit-package-json 0.5.1
Edit package.json without parsing, as string, to keep the formatting intact
📦 easy-replace 5.0.1
Replace strings with optional lookarounds, but without regexes
📦 str-indexes-of-plus 4.0.1
Like indexOf but returns array and counts per-grapheme
📦 js-row-num 5.0.1
Update all row numbers in all console.logs in JS code
📦 line-column-mini 2.0.1
Convert string index to line-column position
📦 email-comb 6.0.1
Remove unused CSS from email templates