-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadder.js
executable file
·168 lines (149 loc) · 4.56 KB
/
padder.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// gotta run as node -r esm filename.js to get import to work
/* For a list of given strings pad out two parts.
1. Any trailing digits get padded with leading zeroes to make the number six digits.
2. The leading two digit year gets padded out to a four digit year.
*/
"use strict;"
import tags from "/Users/evan/Dropbox/code/codingBatJS/helpers/padderDB.js"
/*
** When passed a tag,
** this function returns the same tag
** with either 19 or 20 prefixed onto the year
*/
const fixYear = function(tag) {
return tag.slice(0,2);
}
/*
** When passed NOARGUMENT,
** this function returns the first three tags in the db
*/
const showThirtyRaw = function() {
return tags.slice(0,30);
}
/*
** When passed a one or two digit number,
** returns that as a four digit year
** e.g. passing '4' returns 2004
** e.g. passing '99' returns 1999
*/
const yearExpand = function(s) {
const CURRENTYEAR = 19;
let shortYear = Number(s);
if (shortYear > -1 && shortYear < 99 ){ // legit input
if (shortYear < 10){
return '200' + String(shortYear);
}
if (shortYear <= CURRENTYEAR){
return '20' + String(shortYear);
}
if (shortYear <= 99){
return '19' + String(shortYear);
} else { // should never see this
return '9998'; // returns nonlethal error
}
} else { // bad input: not number, not in range
return '9999' // returns nonlethal error
}
}
/*
** When passed a number of six or less digits,
** returns a six digit number, with
** zeroes as leading padding
** e.g. passing '4' returns '000004'
** e.g. passing '99' returns '000099'
*/
const idExpand = function(s) {
let shortID = String(s);
while(shortID.length < 6){
shortID = '0' + shortID;
}
return shortID;
}
/*
** When passed one entire tag,
** returns just the fixed ID as a string
** e.g. passing '4foo27' returns '4foo000027'
*/
const cleanseID = function(t) {
let reggie = /^(.*[A-z])([0-9]+)$/;
let frags = t.match(reggie); // array[0] is fulltag; array[1]is captured nonID portion; array[2] is trailing digits which are presumably IDs;
return idExpand(frags[2]); // returns a padded iD
}
/*
** When passed one entire tag,
** returns just the fixed year as a string
** e.g. passing '4foo83827' returns 2004
*/
const cleanseYearTag = function(t) {
let reggie = /(^[0-9]+)(.*)/;
let frags = t.match(reggie); // array[0] is fulltag; array[1] is leading year digit; array[2]is captured post-year portion
return yearExpand(frags[1]); // returns a fixed year
}
/*
** When passed a single raw tag STRING,
** returns a tag STRING where all the years have been expanded to 4 digit years and
** all of the trailing ids have been padded to be six digits long
** e.g. passing '4foo7' returns '2004foo000007'
** e.g. passing '90foo2345' returns '1990foo002345'
*/
const cleanseOneTagCompletely = function(t) {
let reggie, // REGEX
frags = [], // parsed pieces of the tag
a, // year
b, // alpha
c; // id
reggie = /(^[0-9]+)([A-z].*[A-z])([0-9]+)$/;
frags = t.match(reggie); // array[0] is fulltag; array[1] is leading year digit; array[2]is captured alphas; array[3] is captured ID number
a = yearExpand(frags[1]);
b = frags[2];
c = idExpand(frags[3]);
return a + b + c; // returns one completely fixed tag as a String
}
const cleanseOneArrayCompletely = function(rawArr) {
let cleanArr = [];
let i = 0,
stop = rawArr.length;
for (; i < stop; i += 1){
cleanArr.push( cleanseOneTagCompletely(rawArr[i]) )
}
return cleanArr;
}
// _
// (_)
// _ __ ___ __ _ _ _ __
// | '_ ` _ \ / _` | | | | '_ \
// | | | | | | | (_| | | | | | | |
// |_| |_| |_| \__,_| |_| |_| |_|
// Detect option. Run Tests.
let option = process.argv[2];
if (option !== undefined){
switch (option){
case 'nada':
console.log(`Running the :::${option}::: test.`);
break;
case 'dbsize':
console.log(`Present ${tags.length}`)
break;
case 'seeyear':
console.log(`Tag starts with these two :::${fixYear(tags[0])}:::`)
break;
case 'showthirtytags':
console.log(`We see_______${showThirtyRaw()}`);
break;
case 'cleanyear':
console.log(`We see_______${cleanseYearTag(tags[0])}`);
break;
case 'cleanid':
console.log(`We see_______${cleanseID(tags[0])}`);
break;
case 'holistictag':
console.log(`We see_______${cleanseOneTagCompletely(tags[0])}`);
case 'holisticarray':
console.log(`We see_______${cleanseOneArrayCompletely(tags)}`);
break;
default:
console.log(`:::${option}::: not recognized.`)
}
} else {
console.log(`To run tests, enter an option besides ${option}`)
}