Skip to content

An ESnext spec-compliant `String.prototype.toWellFormed` shim/polyfill/replacement that works as far down as ES3.

License

Notifications You must be signed in to change notification settings

es-shims/String.prototype.toWellFormed

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Mar 20, 2024
591e597 · Mar 20, 2024

History

22 Commits
Mar 20, 2024
Mar 20, 2024
Dec 20, 2022
Sep 13, 2022
Sep 13, 2022
Sep 13, 2022
Mar 20, 2024
Sep 13, 2022
May 15, 2023
Sep 13, 2022
Mar 20, 2024
Mar 20, 2024
Mar 20, 2024
Sep 13, 2022
Sep 13, 2022

Repository files navigation

string.prototype.towellformed Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

An ESnext spec-compliant String.prototype.toWellFormed shim/polyfill/replacement that works as far down as ES3.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the expected ES2024 spec.

Because String.prototype.toWellFormed depends on a receiver (the this value), the main export takes the string to operate on as the first argument.

Getting started

npm install --save string.prototype.towellformed

Usage/Examples

var toWellFormed = require('string.prototype.towellformed');
var assert = require('assert');

var leadingPoo = '\uD83D';
var trailingPoo = '\uDCA9';
var wholePoo = leadingPoo + trailingPoo;

var replacementChar = '\uFFFD';

assert.equal(toWellFormed(wholePoo), wholePoo);
assert.equal(toWellFormed(leadingPoo), replacementChar);
assert.equal(toWellFormed(trailingPoo), replacementChar);
var toWellFormed = require('string.prototype.towellformed');
var assert = require('assert');
/* when String#toWellFormed is not present */
delete String.prototype.toWellFormed;
var shimmed = toWellFormed.shim();

assert.equal(shimmed, toWellFormed.getPolyfill());
assert.deepEqual(wholePoo.toWellFormed(), toWellFormed(wholePoo));
assert.deepEqual(leadingPoo.toWellFormed(), toWellFormed(leadingPoo));
assert.deepEqual(trailingPoo.toWellFormed(), toWellFormed(trailingPoo));
var toWellFormed = require('string.prototype.towellformed');
var assert = require('assert');
/* when String#at is present */
var shimmed = toWellFormed.shim();

assert.equal(shimmed, String.prototype.toWellFormed);
assert.deepEqual(wholePoo.toWellFormed(), toWellFormed(wholePoo));
assert.deepEqual(leadingPoo.toWellFormed(), toWellFormed(leadingPoo));
assert.deepEqual(trailingPoo.toWellFormed(), toWellFormed(trailingPoo));

Tests

Simply clone the repo, npm install, and run npm test