From 3d02da9043f6bd750cb92df1cd10b2af953d9715 Mon Sep 17 00:00:00 2001 From: Sam Thorogood Date: Wed, 31 Aug 2022 08:28:43 +1000 Subject: [PATCH] migrate to mjs, sub package --- .gitignore | 2 ++ build.mjs => build.js | 0 package.json | 12 ++++++---- package/package.json | 9 +++++++ release.sh | 10 ++++++++ test-setup.mjs => test-setup.js | 0 test.html | 42 +++++++++------------------------ test.mjs => test.js | 29 +++++++++++++++++++---- 8 files changed, 64 insertions(+), 40 deletions(-) rename build.mjs => build.js (100%) create mode 100644 package/package.json create mode 100755 release.sh rename test-setup.mjs => test-setup.js (100%) rename test.mjs => test.js (89%) diff --git a/.gitignore b/.gitignore index 30da823..a26d445 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .DS_Store node_modules/ yarn-error.log + +package/* \ No newline at end of file diff --git a/build.mjs b/build.js similarity index 100% rename from build.mjs rename to build.js diff --git a/package.json b/package.json index c4d99e3..38a1211 100644 --- a/package.json +++ b/package.json @@ -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 ", "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" } diff --git a/package/package.json b/package/package.json new file mode 100644 index 0000000..e767d80 --- /dev/null +++ b/package/package.json @@ -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 ", + "license": "Apache-2.0" +} diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..ec42bb6 --- /dev/null +++ b/release.sh @@ -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 diff --git a/test-setup.mjs b/test-setup.js similarity index 100% rename from test-setup.mjs rename to test-setup.js diff --git a/test.html b/test.html index 0833c89..f45b829 100644 --- a/test.html +++ b/test.html @@ -3,43 +3,23 @@ - - - - - -
- + + + diff --git a/test.mjs b/test.js similarity index 89% rename from test.mjs rename to test.js index f0ec83d..8149920 100644 --- a/test.mjs +++ b/test.js @@ -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 @@ -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'); + }); + } }); }); @@ -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); +}); +