Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zc zhang dev #38

Merged
merged 2 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 10 additions & 65 deletions src/js/processData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatDecimal, formatTime } from './utils';
import { formatTime } from './utils';

export const splitData = (data, platform) => {
if (!data) return;
Expand Down Expand Up @@ -39,79 +39,24 @@ export const splitData = (data, platform) => {
};
};

export const getDepthData = (data, coinType, precision) => {
export const getDepthData = (data, coinType) => {
if (!data || !coinType) return;
let buyData = []; //买入数据
let sellData = []; //卖出数据
let bids = data.bids;
let bidsTotal = 0;
let maxBuyPrice = 0;
let minBuyPrice = 0;
let maxSellPrice = 0;
let minSellPrice = 0;
let buyAmounts = [];
let sellAmounts = [];
let buyPrices = [];
let sellPrices = [];
let sellData = [];
let buyData = [];
let amountsPrecision = !isNaN(precision.amount) ? precision.amount : 2;
let pricePrecision = !isNaN(precision.price) ? precision.price : 6;
let asks = data.asks;
if (Array.isArray(bids) && bids.length > 0) {
let datas = bids.slice(0, 50);
let amounts = [];
let prices = [];
for (let data of datas) {
bidsTotal = bidsTotal + parseFloat(data.amount);
amounts.push(bidsTotal);
prices.push(formatDecimal(data.price, pricePrecision));
for (let bid of bids) {
buyData.push([bid.price, bid.total]);
}
maxBuyPrice = Math.max.apply(null, prices);
minBuyPrice = Math.min.apply(null, prices);
buyAmounts = amounts;
buyPrices = prices;
}
let asks = data.asks;
let asksTotal = 0;
if (Array.isArray(asks) && asks.length > 0) {
let datas = asks.slice(0, 50);
let amounts = [];
let prices = [];
for (let data of datas) {
asksTotal = asksTotal + parseFloat(data.amount);
amounts.push(asksTotal);
prices.push(formatDecimal(data.price, pricePrecision));
for (let ask of asks) {
sellData.push([ask.price, ask.total]);
}
maxSellPrice = Math.max.apply(null, prices);
minSellPrice = Math.min.apply(null, prices);
sellAmounts = amounts;
sellPrices = prices;
}
let priceGap = maxSellPrice - minBuyPrice;
let buyPriceGap = maxBuyPrice - minBuyPrice;
let buyPercent =
buyPriceGap / priceGap < 0.25 ? 0.25 : buyPriceGap / priceGap;
let sellPercent = 1 - buyPercent;
let maxAmount = Math.max(bidsTotal, asksTotal);
for (let index = 0; index < sellPrices.length; index++) {
sellData.push([formatDecimal(sellPrices[index], pricePrecision), formatDecimal(sellAmounts[index], amountsPrecision)]);
}
for (let index = 0; index < buyPrices.length; index++) {
buyData.push([formatDecimal(buyPrices[index], pricePrecision), formatDecimal(buyAmounts[index], amountsPrecision)]);
}
buyData = buyData.reverse();
return {
maxAmount,
maxBuyPrice,
minBuyPrice,
maxSellPrice,
minSellPrice,
buyAmounts,
buyPrices,
sellData,
buyData,
sellPrices,
sellAmounts,
buyPercent,
sellPercent
buyData
};
};

Expand Down
52 changes: 39 additions & 13 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ export const getClientHeight = () => {
};

export const formatDecimal = function (f, n, sep) {
var num = parseFloat(f);
if (!isNumber(num)) {
// var num = parseFloat(f);
n = parseInt(n)
if (isNaN(f)) {
return f;
}
if ((sep || 0) === 0) {
return num.toFixed(n);
return formatNumber(f, n);
}

num = num.toFixed(n);
var num = formatNumber(f, n);
var result = '';

var split = num.split('.');
Expand All @@ -40,18 +41,43 @@ export const formatDecimal = function (f, n, sep) {
return result;
};

export const formatNumber = (value, num) => {
var a, b, c, i;
a = value.toString();
b = a.indexOf('.');
c = a.length;
if (num == 0) {
if (b != -1) {
a = a.substring(0, b);
}
} else {
if (b == -1) {
a = a + '.';
for (i = 1; i <= num; i++) {
a = a + '0';
}
} else {
a = a.substring(0, b + num + 1);
for (i = c; i <= b + num; i++) {
a = a + '0';
}
}
}
return a;
};

export const formatTime = (t) => {
let unixtime = t;
let unixTimestamp = new Date(unixtime);
let Y = unixTimestamp.getFullYear();
let M =
unixTimestamp.getMonth() + 1 >= 10
? unixTimestamp.getMonth() + 1
: '0' + (unixTimestamp.getMonth() + 1);
unixTimestamp.getMonth() + 1 >= 10
? unixTimestamp.getMonth() + 1
: '0' + (unixTimestamp.getMonth() + 1);
let D =
unixTimestamp.getDate() >= 10
? unixTimestamp.getDate()
: '0' + unixTimestamp.getDate();
unixTimestamp.getDate() >= 10
? unixTimestamp.getDate()
: '0' + unixTimestamp.getDate();
let H = unixTimestamp.getHours();
let toDay = Y + '-' + M + '-' + D + ' ' + H + ':00';
return toDay;
Expand All @@ -60,12 +86,12 @@ export const formatTime = (t) => {
export const getLanguage = function () {
let languageType = localStorage.getItem('languageType');
let message;
if(languageType === 'zh') {
if (languageType === 'zh') {
message = require('../i18n/zh-CN');
}else if(languageType === 'en'){
} else if (languageType === 'en') {
message = require('../i18n/en-GB');
} else {
message = require('../i18n/en-GB');
}
}
return message;
};
4 changes: 4 additions & 0 deletions test/unit/specs/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('test utils', () => {
expect(formatDecimal(11232.234234234, 6, false)).toBe("11232.234234")
})

it('test formatDecimal if num is integer', () => {
expect(formatDecimal('12345', 0, true)).toBe("12,345")
})

it('test getClientWidth', () => {
expect(getClientWidth()).not.toBeNull()
})
Expand Down