Skip to content

Commit

Permalink
Merge pull request #4 from NotNinja/develop
Browse files Browse the repository at this point in the history
Release 0.1.2
  • Loading branch information
neocotic authored Jan 25, 2018
2 parents e617018 + 3852f11 commit d43cc1e
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 177 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.nyc_output/
coverage/
node_modules/
*.log

.nyc_output/
coverage/
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
.nyc_output/
coverage/
codecov.yml

test/
.*
AUTHORS.md
CHANGES.md
codecov.yml
CONTRIBUTING.md
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ sudo: false
language: node_js
node_js:
- "8"
- "9"
script:
- npm test
after_success:
- npm run coverage
notifications:
email: false
slack:
rooms:
- secure: Bu+WX8d4eWg+hubGeMZQ5j/mdt0LmRbohp3NO9DQGi2/026Z7N/tGkienEaD8oOQY3ZORaQuGbMCLBHrMWUIAz8kaeMqtvRjj3a8ZGNVZ/2kFLwv2gQnMGkigULZNbDFKRd6I7WkEfOUU4ANsXNDzsfdM3h2jXbIGew922adF0GkQGm8npLAfshj32Mogwe1oU78pVFukW8TcWrL2iZDRaBk2mrPYfTyP3scbdYeOIDDgWvnV0DkXHP2oPI9mMjzuIOBEXF5cBv7wMMHOu1/dtZzN/zrMB1Hm0iFsdb2TT8NgpTjaxb3y0JuEgC+e1TB6VznZ53HwED5G4eDBFE7Nz4iJ1y4rMTOzDxqMhXPOgwD4DOg2JDgZeT3bm1De63fpeM7vbfuZnGAnm5u9NR2ylVcH0yCCfdH0WV0nhA+k384MQH/WJe1xerrIu7kMOh6VyM7BxgDvpROn3vWrkWfPWufXeyX8ZSaA2j0OcYpQ4nfF2dhsmCEMeKhNU5AgdJhCVa97STsLz8lH9RMSrYz95HH/y6Yz3YZ6te+lCZLokNjo3vO89+hoUHCe5obxwxD9zLWsBhtxtKKgzLGn45JSZOA5zr3QNhQlzw8FkDBrVyLlYKUoP2+kW8eZpTM0udRClSrquXf1Kf0pbReKHemrK73arSMesd/FAjM8hRCtIA=
on_failure: change
on_success: change
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## Version 0.1.2, 2018.01.25

* Move Unicode escape and unescape logic out to new `escape-unicode` and `unescape-unicode` modules respectively
* Configure travis to also build against Node.js v9
* Replace `chai` with `assert` in unit tests
* Bump dependencies

## Version 0.1.1, 2017.12.09

* Fix require statements in README examples [#2](https://github.com/NotNinja/nevis/issues/2)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (C) 2017 Alasdair Mercer, !ninja
Copyright (C) 2018 Alasdair Mercer, !ninja

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ $ cat ascii.properties | native2ascii --reverse > utf8.properties

## API

native2ascii(input[, options])
### native2ascii(input[, options])

Converts the specified `input` so that it can be encoded in ASCII by using Unicode escapes ("\uxxxx" notation) for all
characters that are not part of the ASCII character set.
Expand All @@ -97,13 +97,13 @@ This function is useful for properties files containing characters not in ISO-88

A reverse conversion can be performed by enabling the `reverse` option.

### Options
#### Options

| Option | Description | Default |
| --------- | -------------------------------- | ------- |
| `reverse` | Whether to reverse the operation | `false` |

### Examples
#### Examples

Unicode escape characters not in the ASCII character set so that they can be safely written encoded into ASCII:

Expand Down
2 changes: 1 addition & 1 deletion bin/native2ascii
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-native2ascii",
"version": "0.1.1",
"version": "0.1.2",
"description": "Node.js implementation of Java's Native-to-ASCII Converter",
"homepage": "https://github.com/NotNinja/node-native2ascii",
"bugs": {
Expand All @@ -25,16 +25,17 @@
"url": "https://github.com/NotNinja/node-native2ascii.git"
},
"dependencies": {
"commander": "^2.12.2"
"commander": "^2.13.0",
"escape-unicode": "^0.1.0",
"unescape-unicode": "^0.1.0"
},
"devDependencies": {
"chai": "^4.1.2",
"codecov": "^3.0.0",
"eslint": "^4.13.0",
"eslint": "^4.16.0",
"eslint-config-notninja": "^0.2.3",
"mocha": "^4.0.1",
"nyc": "^11.3.0",
"sinon": "^4.1.3",
"mocha": "^5.0.0",
"nyc": "^11.4.1",
"sinon": "^4.2.1",
"tmp": "0.0.33"
},
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion src/native2ascii.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
31 changes: 4 additions & 27 deletions src/unicode/escape.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +22,7 @@

'use strict';

const hexDigits = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' ];
const escapeUnicode = require('escape-unicode');

/**
* Converts all Unicode characters within the specifed <code>input</code> to Unicode escapes ("\uxxxx" notation).
Expand All @@ -37,36 +37,13 @@ function escape(input) {
const code = input.charCodeAt(i);

if (code > 0x7f) {
result += `\\u${toHex(code)}`;
result += escapeUnicode(input, i, i + 1);
} else {
result += input.charAt(i);
result += input[i];
}
}

return result;
}

/**
* Converts the specified character <code>code</code> to a hexadecimal value.
*
* @param {number} code - the character code to be converted
* @return {string} The 4-digit hexadecimal string.
*/
function toHex(code) {
return toHexDigit((code >> 12) & 15) +
toHexDigit((code >> 8) & 15) +
toHexDigit((code >> 4) & 15) +
toHexDigit(code & 15);
}

/**
* Converts the specified <code>nibble</code> to a hexadecimal digit.
*
* @param {number} nibble - the nibble to be converted
* @return {string} The single-digit hexadecimal string.
*/
function toHexDigit(nibble) {
return hexDigits[nibble & 15];
}

module.exports = escape;
2 changes: 1 addition & 1 deletion src/unicode/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
69 changes: 5 additions & 64 deletions src/unicode/unescape.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Alasdair Mercer, !ninja
* Copyright (C) 2018 Alasdair Mercer, !ninja
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,7 +22,7 @@

'use strict';

/* eslint complexity: "off" */
const unescapeUnicode = require('unescape-unicode');

/**
* Converts all Unicode escapes ("\uxxxx" notation) within the specified <code>input</code> into their corresponding
Expand All @@ -38,13 +38,13 @@ function unescape(input) {
let result = '';

for (let i = 0, length = input.length; i < length; i++) {
let ch = input.charAt(i);
let ch = input[i];

if (ch === '\\') {
ch = input.charAt(++i);
ch = input[++i];

if (ch === 'u') {
result += getUnicode(input, i + 1);
result += unescapeUnicode(input, i + 1);
i += 4;
} else {
result += `\\${ch}`;
Expand All @@ -57,63 +57,4 @@ function unescape(input) {
return result;
}

/**
* Attempts to convert the Unicode escape within <code>input</code> at the specified <code>offset</code>.
*
* <code>offset</code> should be the index of the first character after the "\u" prefix of the Unicode escape and will
* result in the offset being increased as it reads in the next four characters within <code>input</code>.
*
* This function will throw an error if the hexadecimal value corresponding to the Unicode escape at the specified
* <code>offset</code> is malformed.
*
* @param {string} input - the string to be converted
* @param {number} offset - the offset of the hexadecimal segment of the Unicode escape from which the Unicode character
* is to be derived relative to <code>input</code>
* @return {string} The Unicode character converted from the escape at <code>offset</code> within <code>input</code>.
* @throws {Error} If the Unicode escape is malformed.
*/
function getUnicode(input, offset) {
let unicode = 0;

for (let i = offset, end = offset + 4; i < end; i++) {
const ch = input.charAt(i);
const code = ch.charCodeAt(0);

switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
unicode = (unicode << 4) + code - 0x30;
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
unicode = (unicode << 4) + 10 + code - 0x41;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
unicode = (unicode << 4) + 10 + code - 0x61;
break;
default:
throw new Error(`Malformed character found in \\uxxxx encoding: ${ch}`);
}
}

return String.fromCharCode(unicode);
}

module.exports = unescape;
Loading

0 comments on commit d43cc1e

Please sign in to comment.