dice-coefficient
Advanced tools
Comparing version 1.0.5 to 2.0.0
27
cli.js
#!/usr/bin/env node | ||
'use strict' | ||
import fs from 'fs' | ||
import {URL} from 'url' | ||
import {diceCoefficient} from './index.js' | ||
var pack = require('./package.json') | ||
var dice = require('.') | ||
/** @type {Object.<string, unknown>} */ | ||
var pack = JSON.parse( | ||
String(fs.readFileSync(new URL('./package.json', import.meta.url))) | ||
) | ||
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) | ||
@@ -16,4 +20,4 @@ } else if (argv.length === 0) { | ||
process.stdin.setEncoding('utf8') | ||
process.stdin.on('data', function(data) { | ||
getEditDistance(data.trim().split(/\s+/g)) | ||
process.stdin.on('data', function (data) { | ||
getEditDistance(String(data).trim().split(/\s+/g)) | ||
}) | ||
@@ -24,5 +28,8 @@ } else { | ||
/** | ||
* @param {Array.<string>} values | ||
*/ | ||
function getEditDistance(values) { | ||
if (values.length === 2) { | ||
console.log(dice(values[0], values[1]) || 0) | ||
console.log(diceCoefficient(values[0], values[1]) || 0) | ||
} else { | ||
@@ -51,9 +58,9 @@ process.stderr.write(help()) | ||
' $ ' + pack.name + ' night nacht', | ||
' # ' + dice('night', 'nacht'), | ||
' # ' + diceCoefficient('night', 'nacht'), | ||
'', | ||
' # output edit distance from stdin', | ||
' $ echo "saturday sunday" | ' + pack.name, | ||
' # ' + dice('saturday', 'sunday') | ||
' # ' + diceCoefficient('saturday', 'sunday') | ||
].join('\n') + '\n' | ||
) | ||
} |
33
index.js
@@ -1,26 +0,29 @@ | ||
'use strict' | ||
import {bigram} from 'n-gram' | ||
var bigrams = require('n-gram').bigram | ||
module.exports = diceCoefficient | ||
// Get the edit-distance according to Dice between two values. | ||
function diceCoefficient(value, alternative) { | ||
var val = String(value).toLowerCase() | ||
/** | ||
* Get the edit-distance according to Dice between two values. | ||
* | ||
* @param {string} value | ||
* @param {string} alternative | ||
* @returns {number} | ||
*/ | ||
export function diceCoefficient(value, alternative) { | ||
var value_ = String(value).toLowerCase() | ||
var alt = String(alternative).toLowerCase() | ||
var left = val.length === 1 ? [val] : bigrams(val) | ||
var right = alt.length === 1 ? [alt] : bigrams(alt) | ||
var leftLength = left.length | ||
var rightLength = right.length | ||
var left = value_.length === 1 ? [value_] : bigram(value_) | ||
var right = alt.length === 1 ? [alt] : bigram(alt) | ||
var index = -1 | ||
var intersections = 0 | ||
/** @type {string} */ | ||
var leftPair | ||
/** @type {string} */ | ||
var rightPair | ||
/** @type {number} */ | ||
var offset | ||
while (++index < leftLength) { | ||
while (++index < left.length) { | ||
leftPair = left[index] | ||
offset = -1 | ||
while (++offset < rightLength) { | ||
while (++offset < right.length) { | ||
rightPair = right[offset] | ||
@@ -38,3 +41,3 @@ | ||
return (2 * intersections) / (leftLength + rightLength) | ||
return (2 * intersections) / (left.length + right.length) | ||
} |
{ | ||
"name": "dice-coefficient", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "Sørensen–Dice coefficient", | ||
@@ -29,4 +29,9 @@ "license": "MIT", | ||
], | ||
"sideEffects": false, | ||
"type": "module", | ||
"bin": "cli.js", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"index.d.ts", | ||
"index.js", | ||
@@ -36,30 +41,24 @@ "cli.js" | ||
"dependencies": { | ||
"n-gram": "^1.0.0" | ||
"n-gram": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"browserify": "^16.0.0", | ||
"execa": "^1.0.0", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.0.0", | ||
"remark-cli": "^7.0.0", | ||
"remark-preset-wooorm": "^6.0.0", | ||
"tape": "^4.0.0", | ||
"tinyify": "^2.0.0", | ||
"xo": "^0.25.0" | ||
"@types/tape": "^4.0.0", | ||
"c8": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^9.0.0", | ||
"remark-preset-wooorm": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.38.0" | ||
}, | ||
"scripts": { | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify . -s diceCoefficient > dice-coefficient.js", | ||
"build-mangle": "browserify . -s diceCoefficient -p tinyify > dice-coefficient.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" | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"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 build && npm run format && npm run test-coverage" | ||
}, | ||
"nyc": { | ||
"check-coverage": true, | ||
"lines": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
}, | ||
"prettier": { | ||
@@ -75,9 +74,6 @@ "tabWidth": 2, | ||
"prettier": true, | ||
"esnext": false, | ||
"rules": { | ||
"unicorn/prefer-includes": "off" | ||
}, | ||
"ignores": [ | ||
"dice-coefficient.js" | ||
] | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
} | ||
}, | ||
@@ -88,3 +84,8 @@ "remarkConfig": { | ||
] | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 100, | ||
"detail": true, | ||
"strict": true | ||
} | ||
} |
@@ -12,2 +12,5 @@ # dice-coefficient | ||
This package is ESM only: Node 12+ is needed to use it and it must be `import`ed | ||
instead of `require`d. | ||
[npm][]: | ||
@@ -21,9 +24,12 @@ | ||
This package exports the following identifiers: `diceCoefficient`. | ||
There is no default export. | ||
```js | ||
var dice = require('dice-coefficient') | ||
import {diceCoefficient} from 'dice-coefficient' | ||
dice('abc', 'abc') // => 1 | ||
dice('abc', 'xyz') // => 0 | ||
dice('night', 'nacht') // => 0.25 | ||
dice('night', 'nacht') === dice('NiGhT', 'NACHT') // => true | ||
diceCoefficient('abc', 'abc') // => 1 | ||
diceCoefficient('abc', 'xyz') // => 0 | ||
diceCoefficient('night', 'nacht') // => 0.25 | ||
diceCoefficient('night', 'nacht') === dice('NiGhT', 'NACHT') // => true | ||
``` | ||
@@ -73,5 +79,5 @@ | ||
[build-badge]: https://img.shields.io/travis/words/dice-coefficient.svg | ||
[build-badge]: https://github.com/words/dice-coefficient/workflows/main/badge.svg | ||
[build]: https://travis-ci.org/words/dice-coefficient | ||
[build]: https://github.com/words/dice-coefficient/actions | ||
@@ -78,0 +84,0 @@ [coverage-badge]: https://img.shields.io/codecov/c/github/words/dice-coefficient.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8462
6
102
100
Yes
10
2
+ Addedn-gram@2.0.2(transitive)
- Removedn-gram@1.1.2(transitive)
Updatedn-gram@^2.0.0