Skip to content

Commit

Permalink
migrate to mjs, sub package
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Aug 30, 2022
1 parent ea6efc7 commit 3d02da9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
node_modules/
yarn-error.log

package/*
File renamed without changes.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "fast-text-encoding",
"version": "1.0.4",
"description": "Fast polyfill for TextEncoder and TextDecoder, only supports utf-8",
"version": "0.0.1",
"private": "true",
"description": "Build package for fast-text-encoding, should not be published",
"main": "text.min.js",
"repository": "https://github.com/samthor/fast-text-encoding.git",
"author": "Sam Thorogood <[email protected]>",
"license": "Apache-2.0",
"scripts": {
"compile": "node ./build.mjs",
"test": "node --test ./test.mjs"
"build": "node ./build.js",
"test": "node --test ./test.js"
},
"devDependencies": {
"@types/node": "^18.7.13",
"esbuild": "^0.15.5"
}
},
"type": "module"
}
9 changes: 9 additions & 0 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "fast-text-encoding",
"version": "1.0.5",
"description": "Fast polyfill for TextEncoder and TextDecoder, only supports utf-8",
"main": "text.min.js",
"repository": "https://github.com/samthor/fast-text-encoding.git",
"author": "Sam Thorogood <[email protected]>",
"license": "Apache-2.0"
}
10 changes: 10 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

set -eu

npm run build
npm run test
cp README.md LICENSE text.min.js* package/
cd package/
npm version patch
npm publish --dry-run
File renamed without changes.
42 changes: 11 additions & 31 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,23 @@
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="node_modules/mocha/mocha.css" />
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script>
window.NativeTextDecoder = window.TextDecoder;
window.NativeTextEncoder = window.TextEncoder;
window.TextDecoder = null;
window.TextEncoder = null;
</script>
<script src="text.js"></script>
<script>

var assert = chai.assert;
mocha.setup({ ui: 'tdd' });
<script type="module">

(function() {
var pageError = null;
import {
NativeTextEncoder,
NativeTextDecoder,
} from './test-setup.js';

window.addEventListener('error', function(event) {
pageError = event.filename + ':' + event.lineno + ' ' + event.message;
});
import './text.min.js';

window.addEventListener('load', function() {
if (pageError) {
suite('page-script-errors', function() {
test('no script errors on page', function() {
assert.fail(null, null, pageError);
});
});
}
mocha.run();
});
})();
window._NativeTextEncoder = NativeTextEncoder;
window._NativeTextDecoder = NativeTextDecoder;

</script>
</head>
<body>
<div id="mocha"></div>
<script src="suite.js"></script>



</body>
</html>
29 changes: 25 additions & 4 deletions test.mjs → test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import {
NativeTextEncoder,
NativeTextDecoder,
} from './test-setup.mjs';
} from './test-setup.js';

import './text.js';
import './text.min.js';

import { decodeFallback, encodeFallback } from './src/lowlevel.js';

import test from 'node:test';
import assert from 'node:assert';
import * as assert from 'node:assert';

/**
* @param {boolean} isNative
Expand Down Expand Up @@ -106,8 +108,15 @@ export async function tests(isNative, TextEncoder, TextDecoder) {
assert.deepEqual(dec.decode(buffer), s);
});

// Since this is being run in Node, this should work.
await test('nodejs encodings', () => {
const d = new TextDecoder('utf16le');
if (typeof window === 'undefined') {
new TextDecoder('utf-16le');
} else {
assert.throws(() => {
new TextDecoder('utf-16le');
});
}
});

});
Expand Down Expand Up @@ -166,3 +175,15 @@ export async function tests(isNative, TextEncoder, TextDecoder) {

await tests(true, NativeTextEncoder, NativeTextDecoder);
await tests(false, TextEncoder, TextDecoder);



await test('always lowlevel', () => {
const src = 'hello there ƒåcé zing';

const b = encodeFallback(src);
const out = decodeFallback(b);

assert.equal(src, out);
});

0 comments on commit 3d02da9

Please sign in to comment.