Skip to content

Commit

Permalink
Initial implementation, tests, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 13, 2022
1 parent fb7377f commit 62e3f55
Show file tree
Hide file tree
Showing 24 changed files with 529 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,

"extends": "@ljharb",

"rules": {
"id-length": 0,
"new-cap": [2, {
"capIsNewExceptions": [
"CodePointAt",
"RequireObjectCoercible",
"SymbolDescriptiveString",
"ToString",
"UTF16EncodeCodePoint",
],
}],
},
}
18 changes: 18 additions & 0 deletions .github/workflows/node-aught.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'Tests: node.js < 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '< 10'
type: minors
command: npm run tests-only

node:
name: 'node < 10'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: 'echo tests completed'
9 changes: 9 additions & 0 deletions .github/workflows/node-esm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'Tests: node.js (ESM)'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node-esm.yml@main
with:
command: npm run tests-esm
7 changes: 7 additions & 0 deletions .github/workflows/node-pretest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'Tests: pretest/posttest'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/pretest.yml@main
18 changes: 18 additions & 0 deletions .github/workflows/node-tens.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'Tests: node.js >= 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 10'
type: minors
command: npm run tests-only

node:
name: 'node >= 10'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: 'echo tests completed'
15 changes: 15 additions & 0 deletions .github/workflows/rebase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Automatic Rebase

on: [pull_request_target]

jobs:
_:
name: "Automatic Rebase"

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: ljharb/rebase@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/require-allow-edits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Require “Allow Edits”

on: [pull_request_target]

jobs:
_:
name: "Require “Allow Edits”"

runs-on: ubuntu-latest

steps:
- uses: ljharb/require-allow-edits@main
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ dist
npm-shrinkwrap.json
package-lock.json
yarn.lock

.npmignore
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
package-lock=false
allow-same-version=true
message=v%s
9 changes: 9 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"exclude": [
"coverage",
"test"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,83 @@
# String.prototype.toWellFormed
# string.prototype.towellformed <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

[![npm badge][npm-badge-png]][package-url]

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

This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the proposed [spec](https://tc39.es/proposal-is-usv-string/).

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

```sh
npm install --save string.prototype.towellformed
```

## Usage/Examples

```js
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);
```

```js
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));
```

```js
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`

[package-url]: https://npmjs.org/package/string.prototype.towellformed
[npm-version-svg]: https://versionbadg.es/es-shims/String.prototype.toWellFormed.svg
[deps-svg]: https://david-dm.org/es-shims/String.prototype.toWellFormed.svg
[deps-url]: https://david-dm.org/es-shims/String.prototype.toWellFormed
[dev-deps-svg]: https://david-dm.org/es-shims/String.prototype.toWellFormed/dev-status.svg
[dev-deps-url]: https://david-dm.org/es-shims/String.prototype.toWellFormed#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/string.prototype.towellformed.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/string.prototype.towellformed.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/string.prototype.towellformed.svg
[downloads-url]: https://npm-stat.com/charts.html?package=string.prototype.towellformed
[codecov-image]: https://codecov.io/gh/es-shims/String.prototype.toWellFormed/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/es-shims/String.prototype.toWellFormed/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/String.prototype.toWellFormed
[actions-url]: https://github.com/es-shims/String.prototype.toWellFormed/actions
3 changes: 3 additions & 0 deletions auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

require('./shim')();
24 changes: 24 additions & 0 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var CodePointAt = require('es-abstract/2022/CodePointAt');
// var UTF16EncodeCodePoint = require('es-abstract/2022/UTF16EncodeCodePoint');
var RequireObjectCoercible = require('es-abstract/2022/RequireObjectCoercible');
var ToString = require('es-abstract/2022/ToString');

module.exports = function toWellFormed() {
var O = RequireObjectCoercible(this); // step 1
var S = ToString(O); // step 2
var strLen = S.length; // step 3
var k = 0; // step 4
var result = ''; // step 5
while (k < strLen) { // step 6
var cp = CodePointAt(S, k); // step 6.a
if (cp['[[IsUnpairedSurrogate]]']) { // step 6.b
result += '\uFFFD'; // step 6.b.i
} else { // step 6.c
result += cp['[[CodePoint]]']; // UTF16EncodeCodePoint(cp['[[CodePoint]]']); // step 6.c.i
}
k += cp['[[CodeUnitCount]]']; // step 6.d
}
return result; // step 7
};
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

var define = require('define-properties');
var RequireObjectCoercible = require('es-abstract/2022/RequireObjectCoercible');
var callBind = require('call-bind');

var implementation = require('./implementation');

var getPolyfill = require('./polyfill');
var bound = callBind(getPolyfill());

var shim = require('./shim');

var boundShim = function toWellFormed(string) {
RequireObjectCoercible(string);
return bound(string);
};
define(boundShim, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = boundShim;
15 changes: 15 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import callBind from 'call-bind';
import RequireObjectCoercible from 'es-abstract/2022/RequireObjectCoercible.js';

import getPolyfill from 'string.prototype.towellformed/polyfill';

const bound = callBind(getPolyfill());

export default function toWellFormed(string) {
RequireObjectCoercible(string);
return bound(string);
}

export { default as getPolyfill } from 'string.prototype.towellformed/polyfill';
export { default as implementation } from 'string.prototype.towellformed/implementation';
export { default as shim } from 'string.prototype.towellformed/shim';
96 changes: 96 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"name": "string.prototype.towellformed",
"version": "0.0.0",
"description": "An ESnext spec-compliant `String.prototype.toWellFormed` shim/polyfill/replacement that works as far down as ES3.",
"main": "index.js",
"exports": {
".": [
{
"import": "./index.mjs",
"require": "./index.js",
"default": "./index.js"
},
"./index.js"
],
"./auto": "./auto.js",
"./polyfill": "./polyfill.js",
"./implementation": "./implementation.js",
"./shim": "./shim.js",
"./package.json": "./package.json"
},
"directories": {
"test": "test"
},
"scripts": {
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest",
"pretest": "npm run lint",
"lint": "eslint --ext=js,mjs .",
"postlint": "es-shim-api --bound",
"tests-only": "nyc tape 'test/**/*.js'",
"tests-esm": "nyc node test/index.mjs",
"test": "npm run tests-only && npm run tests-esm",
"posttest": "aud --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"repository": {
"type": "git",
"url": "git+https://github.com/es-shims/String.prototype.toWellFormed.git"
},
"keywords": [
"javascript",
"ecmascript",
"polyfill",
"shim",
"es-shim API",
"unicode",
"string",
"surrogate",
"pair",
"well-formed",
"String.prototype.toWellFormed"
],
"author": "Jordan Harband <ljharb@gmail.com>",
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/es-shims/String.prototype.toWellFormed/issues"
},
"homepage": "https://github.com/es-shims/String.prototype.toWellFormed#readme",
"devDependencies": {
"@es-shims/api": "^2.2.3",
"@ljharb/eslint-config": "^21.0.0",
"aud": "^2.0.0",
"auto-changelog": "^2.4.0",
"es-value-fixtures": "^1.4.2",
"eslint": "=8.8.0",
"functions-have-names": "^1.2.3",
"has-strict-mode": "^1.0.1",
"npmignore": "^0.3.0",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
"tape": "^5.6.0"
},
"dependencies": {
"call-bind": "^1.0.2",
"define-properties": "^1.1.4",
"es-abstract": "^1.20.2"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
},
"publishConfig": {
"ignore": [
".github/workflows"
]
}
}
7 changes: 7 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return String.prototype.toWellFormed || implementation;
};
Loading

0 comments on commit 62e3f55

Please sign in to comment.