Skip to content

Commit

Permalink
Simplify & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasbynens committed Jul 30, 2021
1 parent 28728a8 commit 210ae91
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 340 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
indent_style = tab
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[{README.md,*.yml,*.d.ts}]
indent_style = space
indent_size = 2
31 changes: 31 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: run-checks

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
strategy:
matrix:
# Include all major maintenance + active LTS + current Node.js versions.
# https://github.com/nodejs/Release#release-schedule
node: [12, 14, 16]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Test
run: npm test
8 changes: 7 additions & 1 deletion .github/workflows/publish-on-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ name: publish-on-tag
on:
push:
tags:
- '*'
- '*'

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Test
run: npm test
- name: Publish
env:
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
Expand Down
93 changes: 0 additions & 93 deletions Gruntfile.js

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# koi8-r [![koi8-r on npm](https://img.shields.io/npm/v/koi8-r)](https://www.npmjs.com/package/koi8-r)
# koi8-r [![Build status](https://github.com/mathiasbynens/koi8-r/workflows/run-checks/badge.svg)](https://github.com/mathiasbynens/koi8-r/actions?query=workflow%3Arun-checks) [![koi8-r on npm](https://img.shields.io/npm/v/koi8-r)](https://www.npmjs.com/package/koi8-r)

_koi8-r_ is a robust JavaScript implementation of [the koi8-r character encoding as defined by the Encoding Standard](https://encoding.spec.whatwg.org/#koi8-r).

Expand Down
5 changes: 1 addition & 4 deletions data/index.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Any copyright is dedicated to the Public Domain.
# https://creativecommons.org/publicdomain/zero/1.0/
#
# For details on index index-koi8-r.txt see the Encoding Standard
# https://encoding.spec.whatwg.org/
#
# Identifier: c5497cd9071cb352c0e56b219154e539badf63de40b71578f09e2e11fe7d50ae
# Date: 2016-01-20
# Date: 2018-01-06

0 0x2500 ─ (BOX DRAWINGS LIGHT HORIZONTAL)
1 0x2502 │ (BOX DRAWINGS LIGHT VERTICAL)
Expand Down
14 changes: 4 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@
"scripts": {
"download": "curl https://encoding.spec.whatwg.org/index-koi8-r.txt > data/index.txt",
"build": "node scripts/transform-data.js",
"test": "node tests/tests.js"
"test": "node tests/tests.js",
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
},
"devDependencies": {
"coveralls": "^2.11.6",
"grunt": "^0.4.5",
"grunt-shell": "^1.1.2",
"grunt-template": "^0.2.3",
"istanbul": "^0.4.2",
"jsesc": "^2.1.0",
"qunit-extras": "^1.4.5",
"qunitjs": "~1.11.0",
"requirejs": "^2.1.22",
"string.fromcodepoint": "^0.2.1"
"jsesc": "^3.0.2",
"lodash.template": "^4.5.0"
}
}
20 changes: 10 additions & 10 deletions scripts/export-data.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var fs = require('fs');
var jsesc = require('jsesc');
const fs = require('fs');
const jsesc = require('jsesc');

function readJSON(path) {
return JSON.parse(fs.readFileSync(path, 'utf-8'));
}

module.exports = {
'labels': jsesc(readJSON('data/labels.json'), {
'compact': false,
'indentLevel': 2
labels: jsesc(readJSON('data/labels.json'), {
compact: false,
indentLevel: 2,
}),
'encoded': jsesc(readJSON('data/encoded.json'), { 'wrap': true }),
'decoded': jsesc(readJSON('data/decoded.json'), { 'wrap': true }),
'indexByCodePoint': jsesc(readJSON('data/index-by-code-point.json')),
'indexByPointer': jsesc(readJSON('data/index-by-pointer.json')),
'version': readJSON('package.json').version
encoded: jsesc(readJSON('data/encoded.json'), { wrap: true }),
decoded: jsesc(readJSON('data/decoded.json'), { wrap: true }),
indexByCodePoint: jsesc(readJSON('data/index-by-code-point.json')),
indexByPointer: jsesc(readJSON('data/index-by-pointer.json')),
version: readJSON('package.json').version
};
60 changes: 38 additions & 22 deletions scripts/transform-data.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
var fs = require('fs');
var jsesc = require('jsesc');
require('string.fromcodepoint');
const fs = require('fs');
const jsesc = require('jsesc');
const template = require('lodash.template');

function format(object) {
return jsesc(object, {
'json': true,
'compact': false
json: true,
compact: false,
}) + '\n';
}

function parse(source) {
var indexByCodePoint = {};
var indexByPointer = {};
var decoded = '';
var encoded = '';
const indexByCodePoint = {};
const indexByPointer = {};
let decoded = '';
let encoded = '';
var lines = source.split('\n');
lines.forEach(function(line) {
var data = line.trim().split('\t');
for (const line of lines) {
const data = line.trim().split('\t');
if (data.length != 3) {
return;
continue;
}
var pointer = Number(data[0]);
var codePoint = Number(data[1]);
var symbol = String.fromCodePoint(codePoint);
const pointer = Number(data[0]);
const codePoint = Number(data[1]);
const symbol = String.fromCodePoint(codePoint);
decoded += symbol;
encoded += String.fromCodePoint(pointer + 0x80);
indexByCodePoint[codePoint] = pointer;
indexByPointer[pointer] = symbol;
});
}
return {
'decoded': decoded,
'encoded': encoded,
'indexByCodePoint': indexByCodePoint,
'indexByPointer': indexByPointer
decoded: decoded,
encoded: encoded,
indexByCodePoint: indexByCodePoint,
indexByPointer: indexByPointer
};
}

var source = fs.readFileSync('./data/index.txt', 'utf-8');
var result = parse(source);
const source = fs.readFileSync('./data/index.txt', 'utf-8');
const result = parse(source);
fs.writeFileSync(
'./data/index-by-code-point.json',
format(result.indexByCodePoint)
Expand All @@ -54,3 +54,19 @@ fs.writeFileSync(
'./data/encoded.json',
format(result.encoded)
);

// tests/tests.src.js → tests/tests.js
const TEST_TEMPLATE = fs.readFileSync('./tests/tests.src.js', 'utf8');
const createTest = template(TEST_TEMPLATE, {
interpolate: /<\%=([\s\S]+?)%\>/g,
});
const testCode = createTest(require('./export-data.js'));
fs.writeFileSync('./tests/tests.js', testCode);

// src/koi8-r.src.js -> koi8-r.js
const LIB_TEMPLATE = fs.readFileSync('./src/koi8-r.src.js', 'utf8');
const createLib = template(LIB_TEMPLATE, {
interpolate: /<\%=([\s\S]+?)%\>/g,
});
const libCode = createLib(require('./export-data.js'));
fs.writeFileSync('./koi8-r.js', libCode);
52 changes: 8 additions & 44 deletions src/koi8-r.js → src/koi8-r.src.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
/*! https://mths.be/koi8-r v<%= version %> by @mathias | MIT license */
;(function(root) {

// Detect free variables `exports`.
var freeExports = typeof exports == 'object' && exports;

// Detect free variable `module`.
var freeModule = typeof module == 'object' && module &&
module.exports == freeExports && module;

// Detect free variable `global`, from Node.js/io.js or Browserified code,
// and use it as `root`.
var freeGlobal = typeof global == 'object' && global;
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
root = freeGlobal;
}

/*--------------------------------------------------------------------------*/
;(function() {

var object = {};
var hasOwnProperty = object.hasOwnProperty;
Expand All @@ -33,7 +17,7 @@
return '&#' + codePoint + ';';
}
// Else, `mode == 'fatal'`.
throw Error();
throw new Error();
};

// https://encoding.spec.whatwg.org/#single-byte-decoder
Expand Down Expand Up @@ -113,32 +97,12 @@
};

var koi8r = {
'encode': encode,
'decode': decode,
'labels': <%= labels %>,
'version': '<%= version %>'
encode: encode,
decode: decode,
labels: <%= labels %>,
version: '<%= version %>',
};

// Some AMD build optimizers, like r.js, check for specific condition patterns
// like the following:
if (
typeof define == 'function' &&
typeof define.amd == 'object' &&
define.amd
) {
define(function() {
return koi8r;
});
} else if (freeExports && !freeExports.nodeType) {
if (freeModule) { // in Node.js, io.js or RingoJS v0.8.0+
freeModule.exports = koi8r;
} else { // in Narwhal or RingoJS v0.7.0-
for (var key in koi8r) {
koi8r.hasOwnProperty(key) && (freeExports[key] = koi8r[key]);
}
}
} else { // in Rhino or a web browser
root.koi8r = koi8r;
}
module.exports = koi8r;

}(this));
}());
Loading

0 comments on commit 210ae91

Please sign in to comment.