Skip to content

Commit

Permalink
Use ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Mar 16, 2021
1 parent 658dff7 commit 8c99b54
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 64 deletions.
3 changes: 0 additions & 3 deletions .gitignore
@@ -1,8 +1,5 @@
.DS_Store
*.log
.nyc_output/
coverage/
node_modules/
direction.js
direction.min.js
yarn.lock
4 changes: 0 additions & 4 deletions .prettierignore
@@ -1,6 +1,2 @@
.nyc_output/
coverage/
*.md
*.json
direction.js
direction.min.js
11 changes: 6 additions & 5 deletions cli.js
@@ -1,14 +1,15 @@
#!/usr/bin/env node
'use strict'
import fs from 'fs'

var pack = require('./package.json')
var direction = require('.')
import {direction} from './index.js'

var pack = JSON.parse(fs.readFileSync('package.json'))

var argv = process.argv.slice(2)

if (argv.indexOf('--help') !== -1 || argv.indexOf('-h') !== -1) {
if (argv.includes('--help') || argv.includes('-h')) {
console.log(help())
} else if (argv.indexOf('--version') !== -1 || argv.indexOf('-v') !== -1) {
} else if (argv.includes('--version') || argv.includes('-v')) {
console.log(pack.version)
} else if (argv.length === 0) {
process.stdin.resume()
Expand Down
27 changes: 7 additions & 20 deletions index.js
@@ -1,26 +1,13 @@
'use strict'

module.exports = direction

var RTL = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'
var LTR =
var rtlRange = '\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC'
var ltrRange =
'A-Za-z\u00C0-\u00D6\u00D8-\u00F6' +
'\u00F8-\u02B8\u0300-\u0590\u0800-\u1FFF\u200E\u2C00-\uFB1C' +
'\uFE00-\uFE6F\uFEFD-\uFFFF'

var rtl = new RegExp('^[^' + LTR + ']*[' + RTL + ']')
var ltr = new RegExp('^[^' + RTL + ']*[' + LTR + ']')

function direction(value) {
value = String(value || '')

if (rtl.test(value)) {
return 'rtl'
}

if (ltr.test(value)) {
return 'ltr'
}
var rtl = new RegExp('^[^' + ltrRange + ']*[' + rtlRange + ']')
var ltr = new RegExp('^[^' + rtlRange + ']*[' + ltrRange + ']')

return 'neutral'
export function direction(value) {
var source = String(value || '')
return rtl.test(source) ? 'rtl' : ltr.test(source) ? 'ltr' : 'neutral'
}
27 changes: 11 additions & 16 deletions package.json
Expand Up @@ -23,29 +23,27 @@
"contributors": [
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"bin": "cli.js",
"files": [
"index.js",
"cli.js"
],
"devDependencies": {
"browserify": "^17.0.0",
"nyc": "^15.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"xo": "^0.38.0"
},
"scripts": {
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"build-bundle": "browserify . -s direction -o direction.js",
"build-mangle": "browserify . -s direction -p tinyify -o direction.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
"test": "npm run format && npm run test-coverage"
},
"nyc": {
"check-coverage": true,
Expand All @@ -63,14 +61,11 @@
},
"xo": {
"prettier": true,
"esnext": false,
"rules": {
"unicorn/no-array-for-each": "off",
"unicorn/prefer-includes": "off"
},
"ignores": [
"direction.js"
]
"no-var": "off",
"prefer-arrow-callback": "off",
"unicorn/no-array-for-each": "off"
}
},
"remarkConfig": {
"plugins": [
Expand Down
23 changes: 13 additions & 10 deletions readme.md
Expand Up @@ -7,34 +7,37 @@

Detect direction: left-to-right, right-to-left, or neutral.

## API
## Install

Install:
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.

```sh
npm install direction
```

Use:
## Use

```js
var direction = require('direction')
import {direction} from 'direction'

direction('A') // => 'ltr'
direction('anglais') // => 'ltr'
direction('بسيطة') // => 'rtl'
direction('@') // => 'neutral'
```

## CLI
## API

Install:
This package exports the following identifiers: `direction`.
There is no default export.

```sh
npm install -g direction
```
### `direction(value)`

Detect the direction of `value` (`string?`).
Returns `'ltr'`, `'rtl'`, or `'neutral'`.

Use:
## CLI

```txt
Usage: direction [options] <words...>
Expand Down
12 changes: 6 additions & 6 deletions test.js
@@ -1,10 +1,10 @@
'use strict'
import fs from 'fs'
import childProcess from 'child_process'
import {PassThrough} from 'stream'
import test from 'tape'
import {direction} from './index.js'

var childProcess = require('child_process')
var PassThrough = require('stream').PassThrough
var test = require('tape')
var pkg = require('./package.json')
var direction = require('.')
var pkg = JSON.parse(fs.readFileSync('package.json'))

var fixtures = [
{input: '0', output: 'neutral'},
Expand Down

0 comments on commit 8c99b54

Please sign in to comment.