Skip to content

Commit

Permalink
feat: 升级@antv/util
Browse files Browse the repository at this point in the history
  • Loading branch information
zengyue committed Jan 17, 2020
1 parent e96c4d8 commit 30babec
Show file tree
Hide file tree
Showing 21 changed files with 443 additions and 354 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"egg"
],
"globals": {
"$": true
"$": true,
"expect": true,
"beforeAll": true,
"afterAll": true
},
"parser": "babel-eslint",
"parserOptions": {
Expand Down
31 changes: 16 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"interactive"
],
"main": "lib/index.js",
"browser": "build/f2.js",
"module": "src/index.js",
"homepage": "https://github.com/antvis/f2",
"author": "https://github.com/orgs/antvis/people",
Expand All @@ -38,7 +37,6 @@
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@lite-js/torch": "~0.2.6",
"babel-eslint": "~7.2.3",
"babel-loader": "^8.0.0",
"babel-plugin-transform-remove-strict-mode": "~0.0.2",
Expand All @@ -49,12 +47,14 @@
"connect": "~3.6.3",
"d3-queue": "~3.0.7",
"debug": "~3.1.0",
"electron": "~1.8.2-beta5",
"electron": "^7.1.9",
"eslint": "^6.7.2",
"eslint-config-egg": "~4.2.0",
"gatsby": "^2.17.7",
"get-port": "~3.1.0",
"gh-pages": "^2.1.1",
"jest": "^24.9.0",
"jest-electron": "^0.1.11",
"jquery": "^3.3.1",
"jszip": "^3.1.5",
"nightmare": "~2.10.0",
Expand All @@ -76,12 +76,9 @@
"site:deploy": "npm run site:build && gh-pages -d public",
"build": "webpack",
"build-lib": "babel src --out-dir lib",
"bundler": "electron ./bundler/app.js",
"ci": "npm run lint && npm run test-all",
"ci": "npm run lint && npm run test",
"compress": "sh ./bin/compress.sh",
"coverage": "npm run coverage-generator && npm run coverage-viewer",
"coverage-generator": "torch --compile --coverage --renderer --source-pattern src/*.js,src/**/*.js --recursive test/unit",
"coverage-viewer": "torch-coverage",
"coverage": "jest --coverage",
"demos": "electron ./demos/app.js",
"demos-web": "node ./demos/app.js --web --port 2048",
"dev": "npm run watch & DEBUG=app:* npm run demos-web",
Expand All @@ -90,25 +87,29 @@
"lint-fix": "eslint --fix ./",
"prepublishOnly": "npm run build-lib && npm run dist",
"screenshot": "DEBUG=app:* ./bin/screenshot.js",
"test-all": "npm run test && npm run test-bug",
"test": "torch --compile --renderer --recursive ./test/unit",
"test-bug": "torch --compile --renderer --recursive ./test/bug",
"test-bug-live": "torch --compile --interactive --recursive ./test/bug",
"test-live": "torch --compile --interactive --recursive ./test/unit",
"test": "jest",
"test-unit": "jest test/unit",
"test-bug": "jest test/bug",
"test-live": "DEBUG_MODE=1 jest --watch",
"watch": "webpack --config webpack-dev.config.js"
},
"pre-commit": {
"run": [
"lint",
"test-all"
"test"
],
"silent": false
},
"jest": {
"runner": "jest-electron/runner",
"testEnvironment": "jest-electron/environment",
"testRegex": "/test/.*-spec\\.js?$"
},
"dependencies": {
"@antv/adjust": "~0.1.1",
"@antv/attr": "~0.1.0",
"@antv/scale": "~0.1.2",
"@antv/util": "~1.2.5",
"@antv/util": "~2.0.6",
"@babel/runtime": "^7.7.7",
"hammerjs": "^2.0.8"
}
Expand Down
140 changes: 140 additions & 0 deletions src/util/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
isArray,
isNil,
each
} from '@antv/util';

function merge(dataArray) {
let rst = [];
for (let i = 0, len = dataArray.length; i < len; i++) {
rst = rst.concat(dataArray[i]);
}
return rst;
}

function values(data, name) {
const rst = [];
const tmpMap = {};
for (let i = 0, len = data.length; i < len; i++) {
const obj = data[i];
const value = obj[name];
if (!isNil(value)) {
if (!isArray(value)) {
if (!tmpMap[value]) {
rst.push(value);
tmpMap[value] = true;
}
} else {
each(value, val => {
if (!tmpMap[val]) {
rst.push(val);
tmpMap[val] = true;
}
});
}
}
}
return rst;
}

function firstValue(data, name) {
let rst = null;
for (let i = 0, len = data.length; i < len; i++) {
const obj = data[i];
const value = obj[name];
if (!isNil(value)) {
if (isArray(value)) {
rst = value[0];
} else {
rst = value;
}
break;
}
}
return rst;
}

function groupToMap(data, fields) {
if (!fields) {
return {
0: data
};
}

const callback = function(row) {
let unique = '_';
for (let i = 0, l = fields.length; i < l; i++) {
unique += row[fields[i]] && row[fields[i]].toString();
}
return unique;
};

const groups = {};
for (let i = 0, len = data.length; i < len; i++) {
const row = data[i];
const key = callback(row);
if (groups[key]) {
groups[key].push(row);
} else {
groups[key] = [ row ];
}
}

return groups;
}

function group(data, fields, appendConditions = {}) {
if (!fields) {
return [ data ];
}
const groups = groupToMap(data, fields);
const array = [];
if (fields.length === 1 && appendConditions[fields[0]]) {
const values = appendConditions[fields[0]];
each(values, value => {
value = '_' + value;
array.push(groups[value]);
});
} else {
for (const i in groups) {
array.push(groups[i]);
}
}

return array;
}

function remove(arr, obj) {
if (!arr) {
return;
}
const index = arr.indexOf(obj);
if (index !== -1) {
arr.splice(index, 1);
}
}

function getRange(values) {
if (!values.length) {
return {
min: 0,
max: 0
};
}
const max = Math.max.apply(null, values);
const min = Math.min.apply(null, values);
return {
min,
max
};
}

export {
merge,
values,
firstValue,
group,
groupToMap,
remove,
getRange
};
Loading

0 comments on commit 30babec

Please sign in to comment.