Comparing version 1.5.0 to 2.2.1
{ | ||
"name": "xy-parser", | ||
"version": "1.5.0", | ||
"version": "2.2.1", | ||
"description": "Parse a text-file and convert it to an array of XY points", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "rollup -c && cheminfo build", | ||
"build-doc": "cheminfo doc", | ||
"eslint": "eslint src", | ||
"eslint-fix": "npm run eslint -- --fix", | ||
"prepublishOnly": "rollup -c", | ||
"test": "run-s testonly eslint", | ||
"test-travis": "eslint src && jest --coverage && codecov", | ||
"testonly": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/cheminfo-js/xy-parser.git" | ||
}, | ||
"keywords": [ | ||
@@ -9,26 +29,23 @@ "xy file parser", | ||
], | ||
"config": { | ||
"name": "xy-parser" | ||
}, | ||
"author": "Luc Patiny", | ||
"repository": "cheminfo-js/xy-parser", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/cheminfo-js/xy-parser/issues" | ||
}, | ||
"homepage": "https://github.com/cheminfo-js/xy-parser", | ||
"license": "MIT", | ||
"main": "./src/index.js", | ||
"directories": { | ||
"lib": "src", | ||
"test": "test" | ||
"homepage": "https://github.com/cheminfo-js/xy-parser#readme", | ||
"jest": { | ||
"testEnvironment": "node" | ||
}, | ||
"scripts": { | ||
"test": "mocha --require should --reporter mocha-better-spec-reporter --recursive", | ||
"build": "cheminfo build" | ||
}, | ||
"devDependencies": { | ||
"cheminfo-tools": "^1.0.2", | ||
"mocha": "^2.2.5", | ||
"mocha-better-spec-reporter": "^2.1.1", | ||
"should": "^7.0.2" | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2", | ||
"cheminfo-tools": "^1.20.2", | ||
"codecov": "^2.3.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-cheminfo": "^1.17.4", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-jest": "^21.17.0", | ||
"eslint-plugin-no-only-tests": "^2.0.0", | ||
"jest": "^20.0.4", | ||
"npm-run-all": "^4.1.3", | ||
"rollup": "^0.45.2" | ||
}, | ||
@@ -35,0 +52,0 @@ "dependencies": { |
@@ -1,42 +0,34 @@ | ||
# XY text file parser | ||
# xy-parser | ||
[![NPM version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
[![David deps][david-image]][david-url] | ||
[![Test coverage][codecov-image]][codecov-url] | ||
[![npm download][download-image]][download-url] | ||
XY file parser allows to convert a text file to an array of XY. | ||
Parse a text-file and convert it to an array of XY points. | ||
## Installation | ||
`$ npm install --save xy-parser` | ||
## Example of use: | ||
## Usage | ||
```js | ||
import {parseXY} from 'xy-parser'; | ||
const data = `1 2 | ||
3 4 | ||
5 6 | ||
7 8`; | ||
const result = parseXY(data); | ||
/* result -> | ||
[ | ||
[ 1, 2 ], | ||
[ 3, 4 ], | ||
[ 5, 6 ], | ||
[ 7, 8 ] | ||
] | ||
*/ | ||
``` | ||
var parseXY = require('xy-parser'); | ||
var result=parseXY.parse('1 2\r3 4'); | ||
``` | ||
Or with options: | ||
## [API Documentation](https://cheminfo-js.github.io/xy-parser/) | ||
``` | ||
var parseXY = require('xy-parser') | ||
var result=XYParser.parse(data, | ||
{ | ||
uniqueX: true, | ||
arrayType: 'xxyy', | ||
normalize: true | ||
} | ||
); | ||
``` | ||
## Options | ||
* normalize : will set the maximal value to 1 | ||
* arrayType : | ||
* 'xxyy' [[x1,x2,x3,...],[y1,y2,y2,...]] | ||
* 'xyxy' [[x1,y1],[x2,y2],[x3,y3], ...]] (default) | ||
* uniqueX : Make the X values unique (works only with xxyy format). If the X value is repeated the sum of Y is done. | ||
## License | ||
@@ -46,9 +38,9 @@ | ||
[npm-image]: https://img.shields.io/npm/v/cheminfo-dummy.svg?style=flat-square | ||
[npm-url]: https://www.npmjs.com/package/cheminfo-dummy | ||
[travis-image]: https://img.shields.io/travis/cheminfo-js/dummy/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/cheminfo-js/dummy | ||
[david-image]: https://img.shields.io/david/cheminfo-js/dummy.svg?style=flat-square | ||
[david-url]: https://david-dm.org/cheminfo-js/dummy | ||
[download-image]: https://img.shields.io/npm/dm/cheminfo-dummy.svg?style=flat-square | ||
[download-url]: https://www.npmjs.com/package/cheminfo-dummy | ||
[npm-image]: https://img.shields.io/npm/v/xy-parser.svg?style=flat-square | ||
[npm-url]: https://www.npmjs.com/package/xy-parser | ||
[travis-image]: https://img.shields.io/travis/cheminfo-js/xy-parser/master.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/cheminfo-js/xy-parser | ||
[codecov-image]: https://img.shields.io/codecov/c/github/cheminfo-js/xy-parser.svg?style=flat-square | ||
[codecov-url]: https://codecov.io/gh/cheminfo-js/xy-parser | ||
[download-image]: https://img.shields.io/npm/dm/xy-parser.svg?style=flat-square | ||
[download-url]: https://www.npmjs.com/package/xy-parser |
155
src/index.js
@@ -1,96 +0,89 @@ | ||
'use strict'; | ||
import uniqueXFunction from 'ml-arrayxy-uniquex'; | ||
var uniqueXFunction = require('ml-arrayxy-uniquex'); | ||
/** | ||
* | ||
* @param text | ||
* @param options | ||
* @param options.arrayType xxyy or xyxy | ||
* @param {boolean} options.normalize=false | ||
* @param {boolean} options.uniqueX | ||
* @param {number} [options.xColumn=0] - A number that specifies the xColumn | ||
* @param {number} [options.yColumn=1] - A number that specifies the yColumn | ||
* @param {number} [options.maxNumberColumns=(Math.max(xColumn, yColumn)+1 || 2)] - A number that specifies the yColumn | ||
* @param {number} [options.minNumberColumns=(Math.max(xColumn, yColumn)+1 || 2)] - A number that specifies the yColumn | ||
* @returns {*[]|Array} | ||
* Parse a text-file and convert it to an array of XY points | ||
* @param {string} text - csv or tsv strings | ||
* @param {object} [options] | ||
* @param {string} [options.arrayType = 'xyxy'] - xxyy or xyxy | ||
* * 'xxyy' `[[x1,x2,x3,...],[y1,y2,y2,...]]` | ||
* * 'xyxy' `[[x1,y1],[x2,y2],[x3,y3], ...]]` | ||
* @param {boolean} [options.normalize = false] - will set the maximum value to 1 | ||
* @param {boolean} [options.uniqueX = false] - Make the X values unique (works only with 'xxyy' format). If the X value is repeated the sum of Y is done. | ||
* @param {number} [options.xColumn = 0] - A number that specifies the x column | ||
* @param {number} [options.yColumn = 1] - A number that specifies the y column | ||
* @param {number} [options.maxNumberColumns = (Math.max(xColumn, yColumn)+1)] - A number that specifies the maximum number of y columns | ||
* @param {number} [options.minNumberColumns = (Math.max(xColumn, yColumn)+1)] - A number that specifies the minimum number of y columns | ||
* @param {boolean} [options.keepInfo = false] - shoud we keep the non numeric lines. In this case the system will return an object {data, info} | ||
* @return {Array<Array<number>>} - check the 'arrayType' option | ||
*/ | ||
export function parseXY(text, options = {}) { | ||
const { | ||
normalize = false, | ||
uniqueX = false, | ||
arrayType = 'xyxy', | ||
xColumn = 0, | ||
yColumn = 1, | ||
keepInfo = false, | ||
maxNumberColumns = Math.max(xColumn, yColumn) + 1, | ||
minNumberColumns = Math.max(xColumn, yColumn) + 1 | ||
} = options; | ||
function parseXY (text, options={}) { | ||
let { | ||
normalize=false, | ||
uniqueX=false, | ||
arrayType='xyxy', | ||
xColumn=0, | ||
yColumn=1, | ||
maxNumberColumns, | ||
minNumberColumns | ||
} = options; | ||
if (!maxNumberColumns) maxNumberColumns=(Math.max(xColumn, yColumn)+1) || 2; | ||
if (!minNumberColumns) minNumberColumns=(Math.max(xColumn, yColumn)+1) || 2; | ||
var lines = text.split(/[\r\n]+/); | ||
var lines = text.split(/[\r\n]+/); | ||
var maxY = Number.MIN_VALUE; | ||
if (arrayType !== 'xxyy' && arrayType !== 'xyxy') { | ||
throw new Error(`unsupported arrayType (${arrayType})`); | ||
} | ||
var counter=0; | ||
var xxyy= (arrayType==='xxyy') ? true : false; | ||
if (xxyy) { | ||
var result = [ | ||
new Array(lines.length), | ||
new Array(lines.length) | ||
]; | ||
} else { | ||
var result = new Array(lines.length); | ||
} | ||
for (var i = 0; i < lines.length; i++) { | ||
var line = lines[i]; | ||
// we will consider only lines that contains only numbers | ||
if (line.match(/[0-9]+/) && line.match(/^[0-9eE,;\. \t-]+$/)) { | ||
line=line.trim(); | ||
var fields = line.split(/[,; \t]+/); | ||
if (fields && fields.length >= minNumberColumns && fields.length <= maxNumberColumns) { | ||
let x = parseFloat(fields[xColumn]); | ||
let y = parseFloat(fields[yColumn]); | ||
var maxY = Number.MIN_VALUE; | ||
var result = [[], []]; | ||
var info = []; | ||
for (var l = 0; l < lines.length; l++) { | ||
var line = lines[l].trim(); | ||
// we will consider only lines that contains only numbers | ||
if (line.match(/[0-9]+/) && line.match(/^[0-9eE,;. \t-]+$/)) { | ||
var fields = line.split(/,[; \t]+|[; \t]+/); | ||
if (fields.length === 1) { | ||
fields = line.split(/[,; \t]+/); | ||
} | ||
if ( | ||
fields && | ||
fields.length >= minNumberColumns && | ||
fields.length <= maxNumberColumns | ||
) { | ||
let x = parseFloat(fields[xColumn].replace(',', '.')); | ||
let y = parseFloat(fields[yColumn].replace(',', '.')); | ||
if (y > maxY) maxY = y; | ||
if (xxyy) { | ||
result[0][counter]=x; | ||
result[1][counter++]=y; | ||
} else { | ||
result[counter++]=[x, y]; | ||
} | ||
} | ||
} | ||
if (y > maxY) maxY = y; | ||
result[0].push(x); | ||
result[1].push(y); | ||
} | ||
} else if (line) { | ||
info.push({ position: result[0].length, value: line }); | ||
} | ||
} | ||
if (xxyy) { | ||
result[0].length=counter; | ||
result[1].length=counter; | ||
} else { | ||
result.length=counter; | ||
if (normalize) { | ||
for (var i = 0; i < result[0].length; i++) { | ||
result[1][i] /= maxY; | ||
} | ||
} | ||
if (normalize) { | ||
if (xxyy) { | ||
for (var i = 0; i < counter; i++) { | ||
result[1][i] /= maxY; | ||
} | ||
} else { | ||
for (var i = 0; i < counter; i++) { | ||
result[i][1] /= maxY; | ||
} | ||
} | ||
if (uniqueX) { | ||
uniqueXFunction(result[0], result[1]); | ||
} | ||
if (arrayType === 'xyxy') { | ||
var newResult = []; | ||
for (let i = 0; i < result[0].length; i++) { | ||
newResult.push([result[0][i], result[1][i]]); | ||
} | ||
result = newResult; | ||
} | ||
if (uniqueX) { | ||
if (! xxyy) throw new Error('Can only make unique X for xxyy format'); | ||
uniqueXFunction(result[0], result[1]) | ||
} | ||
if (!keepInfo) return result; | ||
return result; | ||
}; | ||
parseXY.parse = parseXY; // keep compatibility | ||
module.exports = parseXY; // direct export | ||
return { | ||
info, | ||
data: result | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
14533
9
312
11
46
3
1