-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathethListMerger.js
107 lines (83 loc) · 4.34 KB
/
ethListMerger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const _ = require("lodash");
const Helpers = require("./helpers");
const Formatter = require("./formatter");
const Merger = require("./merger");
module.exports = class EthListMerger {
/**
* @param etherWalletPath {String} - File path for content to merge into (base array)
* @param ethereumListPath {String} - File path for content to be merged from (merging array)
* @returns {Array} - Merged/Combined, formatted, and sorted array of objects
*/
static mergeToEtherWallet(etherWalletPath, ethereumListPath){
let etherWalletList = etherWalletPath && _.isString(etherWalletPath) ? etherWalletPath : "~./etherwallet/app/scripts/tokens/ethTokens.json";
let ethereumListList = ethereumListPath && _.isString(ethereumListPath) ? ethereumListPath : "~./ethereum-lists/tokens/tokens-eth.json";
let mergedList = this.merge(etherWalletList, ethereumListList, "address");
let formatedResults = this.toEtherWalletFormat(mergedList);
let seperator = process.platform=="win32" ? "\\" : "/";
let pathParts = etherWalletPath.split(seperator);
let mergedFileSuffix = pathParts[pathParts.length - 1];
this.toFile("merged-" + mergedFileSuffix, formatedResults);
}
/**
* @param ethereumListPath {String} - File path for content to merge into (base array)
* @param etherWalletPath {String} - File path for content to be merged from (merging array)
* @returns {Array} - Merged/Combined, formatted, and sorted array of objects
*/
static mergeToEthereumLists(ethereumListPath, etherWalletPath){
let etherWalletList = ethereumListPath ? ethereumListPath : "./etherwallet/app/scripts/tokens/ethTokens.json";
let ethereumListList = etherWalletPath ? etherWalletPath : "./ethereum-lists/tokens/tokens-eth.json";
let mergedList = this.merge(ethereumListList, etherWalletList, "address");
let formatedResults = this.toEthereumListFormat(mergedList);
let seperator = process.platform=="win32" ? "\\" : "/";
let pathParts = etherWalletPath.split(seperator);
let mergedFileSuffix = pathParts[pathParts.length - 1];
this.toFile("merged-" + mergedFileSuffix, formatedResults);
}
/**
* @param mergeInto {Array<Object>|String} - File path or array of objects to merge into (base array)
* @param mergeFrom {Array<Object>|String} - File path or array of objects to be merged from (merging array)
* @param mergeOn {String} - Property name on which to preform existence comparison for merge determination
* @param compareProcessor {Function} - function to act on the mergeOn value for comparison
* @returns {Array} - Merged/Combined array of objects
*/
static merge(mergeInto, mergeFrom, mergeOn, compareProcessor) {
let tokenListInto = _.isString(mergeInto) ? require(mergeInto) : mergeInto;
let tokenListFrom = _.isString(mergeFrom) ? require(mergeFrom) : mergeFrom;
let merger = new Merger(mergeOn, compareProcessor);
return merger.doMerge(tokenListInto, tokenListFrom);
}
/**
* @param mergeInto {Array<Object>|String} - FileName or array of objects to merge into (base array)
* @param mergeFrom {Array<Object>|String} - FileName or array of objects to be merged from (merging array)
* @param mergeOn {String} - Property name on which to preform existence comparison for merge determination
* @param compareProcessor {Function} - function to act on the mergeOn value for comparison
* @returns {Array} - Array of objects that are present in the merging array and not present in the base array
*/
static getDifference(mergeInto, mergeFrom, mergeOn, compareProcessor){
let tokenListInto = _.isString(mergeInto) ? require(mergeInto) : mergeInto;
let tokenListFrom = _.isString(mergeFrom) ? require(mergeFrom) : mergeFrom;
let merger = new Merger(mergeOn, compareProcessor);
return merger.getMergingDiff(tokenListInto, tokenListFrom);
}
/**
* @param valArray {Array<Object>} - Array of objects to format
* @returns {Array} - Formatted array of objects
*/
static toEtherWalletFormat(valArray){
return Formatter.toEtherWalletFormat(valArray);
}
/**
* @param valArray {Array<Object>} - Array of objects to format
* @returns {Array} - Formatted array of objects
*/
static toEthereumListFormat(valArray){
return Formatter.toEthereumListFormat(valArray);
}
/**
* @param filename {String}
* @param result {Array<Object>} - Array of objects to be written to file
*/
static toFile(filename, result) {
Helpers.toFile(filename, result);
}
};