Skip to content

Commit

Permalink
refactor: rewrite with ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Feb 13, 2020
1 parent e00a93d commit 12f804f
Show file tree
Hide file tree
Showing 24 changed files with 492 additions and 569 deletions.
9 changes: 9 additions & 0 deletions .commitlintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
rules: {
'type-enum': [
2,
'always',
['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'style', 'test', 'wip'],
],
},
};
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: ['https://hust.cc', 'https://paypal.me/hustcc', 'https://atool.vip']
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: build

on: [push]

jobs:
build:

runs-on: macOS-latest

strategy:
matrix:
node-version: [8.x, 10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: ci
run: |
npm install
npm run build
npm run test
- name: coverall
if: success()
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
23 changes: 20 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
.project
.settings
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Sys
.DS_STORE
.idea

# Node
node_modules/

# Build
lib
esm

# Test
coverage
node_modules/*
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": true,
"printWidth": 120,
"arrowParens": "always"
}
8 changes: 0 additions & 8 deletions .travis.yml

This file was deleted.

50 changes: 26 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

> **Xmorse** is a pure javascript(~1.5kb) library for encoding / decoding morse code messages, **unicode supported**.
[中文说明文档](README_ZH.md) | [Online DEMO 地址](https://atool.vip/#/morse)
[中文说明文档](README_ZH.md) | [Online DEMO 地址](https://atool.vip/morse)

[![Build Status](https://travis-ci.org/hustcc/xmorse.svg?branch=master)](https://travis-ci.org/hustcc/xmorse) [![Coverage Status](https://coveralls.io/repos/github/hustcc/xmorse/badge.svg?branch=master)](https://coveralls.io/github/hustcc/xmorse?branch=master) [![npm](https://img.shields.io/npm/v/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse) [![npm](https://img.shields.io/npm/dt/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse) [![npm](https://img.shields.io/npm/l/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse)
[![Build Status](https://github.com/hustcc/xmorse/workflows/build/badge.svg)](https://github.com/hustcc/xmorse/actions)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/xmorse/badge.svg?branch=master)](https://coveralls.io/github/hustcc/xmorse?branch=master)
[![npm](https://img.shields.io/npm/v/xmorse.svg)](https://www.npmjs.com/package/xmorse)
[![npm](https://img.shields.io/npm/dm/xmorse.svg)](https://www.npmjs.com/package/xmorse)
[![npm](https://img.shields.io/npm/l/xmorse.svg)](https://www.npmjs.com/package/xmorse)


# 1. Install
Expand All @@ -23,58 +27,56 @@ Or download `dist/xmorse.min.js` source file。

- `ES6` style.

```js
var xmorse = require('xmorse');

//or

import xmorse from 'xmorse';
```ts
import { decode, encode } from 'xmorse';
```


# 3. Usage & API

There is only 2 API named `encode`, `decode`.

For `encode(msg, [option])`, example:
There is only 2 API named `encode`, `decode`. For `encode(msg, [option])`, example:

```js
```ts
import { decode, encode } from 'xmorse';
// standart morse
xmorse.encode('Hello, Xmorse!');
encode('Hello, Xmorse!');

// unicode
xmorse.encode('コンニチハ, セカイ!');
xmorse.encode('越过长城,走向世界');
encode('コンニチハ, セカイ!');
encode('越过长城,走向世界');

// option
var option = {
const option = {
space: ' ',
long: '-',
short: '*'
};
xmorse.encode('越过长城,走向世界', option);
encode('越过长城,走向世界', option);
```

For `decode(morse, [option])`, example:

```js
xmorse.decode('../.-../---/...-/./-.--/---/..-/-/---/---/--...-....-...-/-..---..-.-----/---..-...--...-/-..----.--.....');
```ts
import { decode, encode } from 'xmorse';
decode('../.-../---/...-/./-.--/---/..-/-/---/---/--...-....-...-/-..---..-.-----/---..-...--...-/-..----.--.....');

// option
var option = {
const option = {
space: ' ',
long: '-',
short: '*'
};
xmorse.decode('*-** --- ***- *', option);
decode('*-** --- ***- *', option);
```


# 4. Test

> npm install
>
> npm test
```bash
$npm install

$npm test
```


# 5. LICENSE
Expand Down
51 changes: 28 additions & 23 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

> **Xmorse** 是一个纯 JavaScript 开发仅仅只有 1.5kb 的摩斯密码库,支持浏览器端和 nodejs,支持 unicode 字符串,支持中文 morse 密码编码。
[English Document](README.md) | [在线 DEMO 地址](https://atool.vip/#/morse)
[English Document](README.md) | [在线 DEMO 地址](https://atool.vip/morse)

[![Build Status](https://github.com/hustcc/xmorse/workflows/build/badge.svg)](https://github.com/hustcc/xmorse/actions)
[![Coverage Status](https://coveralls.io/repos/github/hustcc/xmorse/badge.svg?branch=master)](https://coveralls.io/github/hustcc/xmorse?branch=master)
[![npm](https://img.shields.io/npm/v/xmorse.svg)](https://www.npmjs.com/package/xmorse)
[![npm](https://img.shields.io/npm/dm/xmorse.svg)](https://www.npmjs.com/package/xmorse)
[![npm](https://img.shields.io/npm/l/xmorse.svg)](https://www.npmjs.com/package/xmorse)

[![Build Status](https://travis-ci.org/hustcc/xmorse.svg?branch=master)](https://travis-ci.org/hustcc/xmorse) [![Coverage Status](https://coveralls.io/repos/github/hustcc/xmorse/badge.svg?branch=master)](https://coveralls.io/github/hustcc/xmorse?branch=master) [![npm](https://img.shields.io/npm/v/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse) [![npm](https://img.shields.io/npm/dt/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse) [![npm](https://img.shields.io/npm/l/xmorse.svg?style=flat-square)](https://www.npmjs.com/package/xmorse)


# 1. 下载安装
Expand All @@ -22,59 +27,59 @@
<script type="text/javascript" src="dist/xmorse.min.js"></script>
```

- `require` 语法风格.

```js
var xmorse = require('xmorse');

//or
- `import` 语法风格.

import xmorse from 'xmorse';
```ts
import { encode, decode } from 'xmorse';
```


# 3. 使用 & API 接口

这个库仅仅只有两个 API 方法,分别为: `encode``decode`
这个库仅仅只有两个 API 方法,分别为: `encode``decode`对于 API `encode(msg, [option])`,例子如下:

对于 API `encode(msg, [option])`,例子如下:
```ts
import { encode, decode } from 'xmorse';

```js
// 编码标准 摩斯密码
xmorse.encode('Hello, Xmorse!');
encode('Hello, Xmorse!');

// 对于 unicode 编码
xmorse.encode('越过长城,走向世界');
encode('越过长城,走向世界');

// option 配置
var option = {
const option = {
space: ' ',
long: '-',
short: '*'
};
xmorse.encode('越过长城,走向世界', option);
encode('越过长城,走向世界', option);
```

对于 API `decode(morse, [option])`,例子如下:

```js
xmorse.decode('../.-../---/...-/./-.--/---/..-/-/---/---/--...-....-...-/-..---..-.-----/---..-...--...-/-..----.--.....');
```ts
import { encode, decode } from 'xmorse';

decode('../.-../---/...-/./-.--/---/..-/-/---/---/--...-....-...-/-..---..-.-----/---..-...--...-/-..----.--.....');

// option 配置
var option = {
const option = {
space: ' ',
long: '-',
short: '*'
};
xmorse.decode('*-** --- ***- *', option);
decode('*-** --- ***- *', option);
```


# 4. 测试开发

> npm install
>
> npm test
```bash
$npm install

$npm test
```


# 5. 开源协议
Expand Down
61 changes: 61 additions & 0 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { encode, decode } from '../src';
import { STANDARD } from '../src/const';
import casual from 'casual';

describe('XMorse', function() {
it('test morse unicode.', function() {
// unicode
expect(encode('I love you, 我爱你。')).toBe(
'../.-../---/...-/./-.--/---/..-/--..--/--...-....-...-/---..-...--...-/-..----.--...../--..........-.',
);
expect(
decode('../.-../---/...-/./-.--/---/..-/--..--/--...-....-...-/---..-...--...-/-..----.--...../--..........-.'),
).toBe('ILOVEYOU,我爱你。');
});

it('test morse STANDARD.', function() {
for (var k in STANDARD) {
expect(encode(k)).toBe(STANDARD[k].replace(/0/g, '.').replace(/1/g, '-'));
expect(decode(STANDARD[k].replace(/0/g, '.').replace(/1/g, '-'))).toBe(k);
}
// combine
expect(encode('I love you.')).toBe('../.-../---/...-/./-.--/---/..-/.-.-.-');
expect(decode('../.-../---/...-/./-.--/---/..-/.-.-.-')).toBe('ILOVEYOU.');
});

it('test xmorse random text.', function() {
// random test
var w;
for (var i = 10000; i >= 0; i--) {
w = casual.sentence;
expect(w.replace(/\s+/g, '').toLocaleUpperCase()).toBe(decode(encode(w)));
}
});

it('test xmorse option.', function() {
// test options
var option = {
space: ' ',
long: '-',
short: '*',
};
expect(encode('I love you. 我爱你', option)).toBe(
'** *-** --- ***- * -*-- --- **- *-*-*- --***-****-***- ---**-***--***- -**----*--*****',
);
expect(
decode('** *-** --- ***- * -*-- --- **- *-*-*- --***-****-***- ---**-***--***- -**----*--*****', option),
).toBe('ILOVEYOU.我爱你');

expect(encode('Hello', { space: ' ' })).toBe('.... . .-.. .-.. ---');
expect(decode('.... . .-.. .-.. ---', { space: ' ' })).toBe('HELLO');
});

it('test some cases.', function() {
// test some cases
expect(encode('')).toBe('');
expect(decode('')).toBe('');

expect(decode('a')).toBe('');
expect(decode(' ')).toBe('');
});
});
Loading

0 comments on commit 12f804f

Please sign in to comment.