@stackedapp/utils 1.0.2 → 1.9.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/currency.d.ts +23 -0
- package/dist/currency.js +55 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -0
- package/dist/player_data.d.ts +8 -0
- package/dist/player_data.js +36 -0
- package/dist/player_snapshot.d.ts +6 -0
- package/dist/player_snapshot.js +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
declare const currency: {
|
|
2
|
+
unitValue(roundDecimalPlaces?: number): number;
|
|
3
|
+
format(val: number, roundDecimalPlaces?: number): string;
|
|
4
|
+
/**
|
|
5
|
+
* round to a string value suitable for parsing by BigNumber
|
|
6
|
+
* @param val
|
|
7
|
+
* @param roundDecimalPlaces
|
|
8
|
+
* @returns string representing the decimal number, without thousands separator.
|
|
9
|
+
*/
|
|
10
|
+
roundToString(val: number, roundDecimalPlaces?: number): string;
|
|
11
|
+
roundUp(val: number, roundDecimalPlaces?: number): number;
|
|
12
|
+
roundDown(val: number, roundDecimalPlaces?: number): number;
|
|
13
|
+
/**
|
|
14
|
+
* round down to smallest unit that is not zero (so, round up if less than smallest unit)
|
|
15
|
+
*/
|
|
16
|
+
roundDownToMin(val: number, roundDecimalPlaces?: number): number;
|
|
17
|
+
round(val: number, roundDecimalPlaces?: number): number;
|
|
18
|
+
roundUpDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
19
|
+
roundDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
20
|
+
roundDownDecimalPlaces(val: number, decimalPlaces: number): number;
|
|
21
|
+
};
|
|
22
|
+
export default currency;
|
|
23
|
+
//# sourceMappingURL=currency.d.ts.map
|
package/dist/currency.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const currency = {
|
|
4
|
+
// get the minimum unit value, default to a whole number if not specified
|
|
5
|
+
unitValue(roundDecimalPlaces = 0) {
|
|
6
|
+
return 1 / 10 ** (roundDecimalPlaces ?? 0);
|
|
7
|
+
},
|
|
8
|
+
format(val, roundDecimalPlaces = 0) {
|
|
9
|
+
return val.toLocaleString(undefined, {
|
|
10
|
+
maximumFractionDigits: roundDecimalPlaces ?? 0,
|
|
11
|
+
});
|
|
12
|
+
},
|
|
13
|
+
/**
|
|
14
|
+
* round to a string value suitable for parsing by BigNumber
|
|
15
|
+
* @param val
|
|
16
|
+
* @param roundDecimalPlaces
|
|
17
|
+
* @returns string representing the decimal number, without thousands separator.
|
|
18
|
+
*/
|
|
19
|
+
roundToString(val, roundDecimalPlaces = 0) {
|
|
20
|
+
return val.toFixed(roundDecimalPlaces ?? 0);
|
|
21
|
+
},
|
|
22
|
+
roundUp(val, roundDecimalPlaces = 0) {
|
|
23
|
+
return currency.roundUpDecimalPlaces(val, roundDecimalPlaces ?? 0);
|
|
24
|
+
},
|
|
25
|
+
roundDown(val, roundDecimalPlaces = 0) {
|
|
26
|
+
return currency.roundDownDecimalPlaces(val, roundDecimalPlaces ?? 0);
|
|
27
|
+
},
|
|
28
|
+
/**
|
|
29
|
+
* round down to smallest unit that is not zero (so, round up if less than smallest unit)
|
|
30
|
+
*/
|
|
31
|
+
roundDownToMin(val, roundDecimalPlaces = 0) {
|
|
32
|
+
const result = currency.roundDownDecimalPlaces(val, roundDecimalPlaces ?? 0);
|
|
33
|
+
if (result === 0) {
|
|
34
|
+
return 1 / 10 ** (roundDecimalPlaces ?? 0);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
},
|
|
38
|
+
round(val, roundDecimalPlaces = 0) {
|
|
39
|
+
return currency.roundDecimalPlaces(val, roundDecimalPlaces ?? 0);
|
|
40
|
+
},
|
|
41
|
+
roundUpDecimalPlaces(val, decimalPlaces) {
|
|
42
|
+
const factor = 10 ** decimalPlaces;
|
|
43
|
+
return Math.ceil(val * factor) / factor;
|
|
44
|
+
},
|
|
45
|
+
roundDecimalPlaces(val, decimalPlaces) {
|
|
46
|
+
const factor = 10 ** decimalPlaces;
|
|
47
|
+
return Math.round(val * factor) / factor;
|
|
48
|
+
},
|
|
49
|
+
roundDownDecimalPlaces(val, decimalPlaces) {
|
|
50
|
+
const factor = 10 ** decimalPlaces;
|
|
51
|
+
return Math.floor(val * factor) / factor;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
exports.default = currency;
|
|
55
|
+
//# sourceMappingURL=currency.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,7 @@ export * from './conditions';
|
|
|
2
2
|
export * from './template';
|
|
3
3
|
export * from './dynamic';
|
|
4
4
|
export * from './blockchain_utils';
|
|
5
|
+
export { default as currency } from './currency';
|
|
6
|
+
export * from './player_data';
|
|
7
|
+
export * from './player_snapshot';
|
|
5
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -13,9 +13,17 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
16
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.currency = void 0;
|
|
17
21
|
__exportStar(require("./conditions"), exports);
|
|
18
22
|
__exportStar(require("./template"), exports);
|
|
19
23
|
__exportStar(require("./dynamic"), exports);
|
|
20
24
|
__exportStar(require("./blockchain_utils"), exports);
|
|
25
|
+
var currency_1 = require("./currency");
|
|
26
|
+
Object.defineProperty(exports, "currency", { enumerable: true, get: function () { return __importDefault(currency_1).default; } });
|
|
27
|
+
__exportStar(require("./player_data"), exports);
|
|
28
|
+
__exportStar(require("./player_snapshot"), exports);
|
|
21
29
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StackedEntityCurrencies } from '@stackedapp/types';
|
|
2
|
+
/**
|
|
3
|
+
* Aggregates currencies from multiple PlayerData objects into a single combined currency record.
|
|
4
|
+
* Sums up balances, in (deposits), and out (withdrawals) values.
|
|
5
|
+
* Uses the most recent lastUpdated timestamp for each currency.
|
|
6
|
+
*/
|
|
7
|
+
export declare function aggregatePlayerDataCurrencies(playerDatas: Array<StackedEntityCurrencies> | undefined | null): StackedEntityCurrencies;
|
|
8
|
+
//# sourceMappingURL=player_data.d.ts.map
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.aggregatePlayerDataCurrencies = aggregatePlayerDataCurrencies;
|
|
4
|
+
/**
|
|
5
|
+
* Aggregates currencies from multiple PlayerData objects into a single combined currency record.
|
|
6
|
+
* Sums up balances, in (deposits), and out (withdrawals) values.
|
|
7
|
+
* Uses the most recent lastUpdated timestamp for each currency.
|
|
8
|
+
*/
|
|
9
|
+
function aggregatePlayerDataCurrencies(playerDatas) {
|
|
10
|
+
const aggregatedCurrencies = {};
|
|
11
|
+
playerDatas?.forEach((playerData) => {
|
|
12
|
+
Object.entries(playerData).forEach(([currencyId, currencyData]) => {
|
|
13
|
+
if (!aggregatedCurrencies[currencyId]) {
|
|
14
|
+
aggregatedCurrencies[currencyId] = {
|
|
15
|
+
balance: 0,
|
|
16
|
+
in: 0,
|
|
17
|
+
out: 0,
|
|
18
|
+
lastUpdated: currencyData.lastUpdated,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
aggregatedCurrencies[currencyId].balance += currencyData.balance || 0;
|
|
22
|
+
aggregatedCurrencies[currencyId].in =
|
|
23
|
+
(aggregatedCurrencies[currencyId].in || 0) + (currencyData.in || 0);
|
|
24
|
+
aggregatedCurrencies[currencyId].out =
|
|
25
|
+
(aggregatedCurrencies[currencyId].out || 0) + (currencyData.out || 0);
|
|
26
|
+
// Use the most recent lastUpdated timestamp
|
|
27
|
+
if (currencyData.lastUpdated &&
|
|
28
|
+
(!aggregatedCurrencies[currencyId].lastUpdated ||
|
|
29
|
+
currencyData.lastUpdated > (aggregatedCurrencies[currencyId].lastUpdated || 0))) {
|
|
30
|
+
aggregatedCurrencies[currencyId].lastUpdated = currencyData.lastUpdated;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return aggregatedCurrencies;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=player_data.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deconstructGamePlayerId = exports.gamePlayerId = void 0;
|
|
4
|
+
const gamePlayerId = (gameId, playerId) => {
|
|
5
|
+
return `${gameId}:${playerId}`;
|
|
6
|
+
};
|
|
7
|
+
exports.gamePlayerId = gamePlayerId;
|
|
8
|
+
const deconstructGamePlayerId = (gamePlayerId) => {
|
|
9
|
+
const [gameId, playerId] = gamePlayerId.split(':');
|
|
10
|
+
return { gameId, playerId };
|
|
11
|
+
};
|
|
12
|
+
exports.deconstructGamePlayerId = deconstructGamePlayerId;
|
|
13
|
+
//# sourceMappingURL=player_snapshot.js.map
|