Skip to content

Commit d18e52c

Browse files
authored
Merge pull request #22 from trantlabs/date
2 parents 3749fc6 + 3d96c71 commit d18e52c

File tree

2 files changed

+110
-32
lines changed

2 files changed

+110
-32
lines changed

src/Date.ts

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,48 @@ define(Date, {
77
});
88

99
define(Date.prototype, {
10-
setTimezone: function (timezone: string) {
10+
setTimezone: function (this: Date, timezone: string) {
1111
return new Date(
1212
this.toLocaleString("en-US", {
1313
timeZone: timezone,
1414
})
1515
);
1616
},
17-
isInPast: function () {
17+
isInPast: function (this: Date) {
1818
const today = new Date();
1919
today.setHours(0, 0, 0, 0);
2020
return this < today;
2121
},
22+
addYear: function (this: Date, years: number, month: number = 0, date: number = 0) {
23+
return this.setFullYear(this.getFullYear() + years, this.getMonth() + month, this.getDate() + date);
24+
},
25+
addMonth: function (this: Date, months: number, date: number = 0) {
26+
return this.setMonth(this.getMonth() + months, this.getDate() + date);
27+
},
28+
addDate: function (this: Date, days: number) {
29+
return this.setDate(this.getDate() + days);
30+
},
31+
addHours: function (this: Date, hours: number, minutes: number = 0, seconds: number = 0, milliseconds: number = 0) {
32+
return this.setHours(
33+
this.getHours() + hours,
34+
this.getMinutes() + minutes,
35+
this.getSeconds() + seconds,
36+
this.getMilliseconds() + milliseconds
37+
);
38+
},
39+
addMinutes: function (this: Date, minutes: number, seconds: number = 0, milliseconds: number = 0) {
40+
return this.setMinutes(
41+
this.getMinutes() + minutes,
42+
this.getSeconds() + seconds,
43+
this.getMilliseconds() + milliseconds
44+
);
45+
},
46+
addSeconds: function (this: Date, seconds: number, milliseconds: number = 0) {
47+
return this.setSeconds(this.getSeconds() + seconds, this.getMilliseconds() + milliseconds);
48+
},
49+
addMilliseconds: function (this: Date, milliseconds: number) {
50+
return this.setMilliseconds(this.getMilliseconds() + milliseconds);
51+
},
2252
});
2353

2454
declare global {
@@ -27,7 +57,7 @@ declare global {
2757
* Returns the current timestamp as its representation in seconds
2858
* @returns {number} date in seconds
2959
* @example
30-
* Date.nowSeconds() // 1671621321
60+
* Date.nowSeconds()
3161
*/
3262
nowSeconds(): number;
3363
}
@@ -51,6 +81,55 @@ declare global {
5181
* new Date("2022-12-01").isInPast() // returns: true
5282
*/
5383
isInPast(): boolean;
84+
/**
85+
* Add a number of years to the current date
86+
* @returns {number} timestamp in milliseconds
87+
* @example
88+
* new Date().addYear(1)
89+
*/
90+
addYear(years: number, month?: number, date?: number): number;
91+
/**
92+
* Add a number of months to the current date
93+
* @returns {number} timestamp in milliseconds
94+
* @example
95+
* new Date().addMonth(1)
96+
*/
97+
addMonth(months: number, date?: number): number;
98+
/**
99+
* Add a number of days to the current date
100+
* @returns {number} timestamp in milliseconds
101+
* @example
102+
* new Date().addDate(1)
103+
*/
104+
addDate(days: number): number;
105+
/**
106+
* Add a number of hours to the current date
107+
* @returns {number} timestamp in milliseconds
108+
* @example
109+
* new Date().addHours(1)
110+
*/
111+
addHours(hours: number, minutes?: number, seconds?: number, milliseconds?: number): number;
112+
/**
113+
* Add a number of minutes to the current date
114+
* @returns {number} timestamp in milliseconds
115+
* @example
116+
* new Date().addMinutes(1)
117+
*/
118+
addMinutes(minutes: number, seconds?: number, milliseconds?: number): number;
119+
/**
120+
* Add a number of seconds to the current date
121+
* @returns {number} timestamp in milliseconds
122+
* @example
123+
* new Date().addSeconds(1)
124+
*/
125+
addSeconds(seconds: number, milliseconds?: number): number;
126+
/**
127+
* Add a number of milliseconds to the current date
128+
* @returns {number} timestamp in milliseconds
129+
* @example
130+
* new Date().addMilliseconds(1)
131+
*/
132+
addMilliseconds(milliseconds: number): number;
54133
}
55134
}
56135

src/String.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,38 @@ define(String.prototype, {
1414
replaceAll: function (find: string, replace: string) {
1515
return this.replace(new RegExp(escapeRegExp(find), "g"), replace);
1616
},
17-
similarity: function (second: string) {
18-
const first = this.replace(/\s+/g, "");
19-
second = second.replace(/\s+/g, "");
20-
if (!first.length && !second.length) return 1; // if both are empty strings
21-
if (!first.length || !second.length) return 0; // if only one is empty string
22-
if (first === second) return 1; // identical
23-
if (first.length === 1 && second.length === 1) return 0; // both are 1-letter strings
24-
if (first.length < 2 || second.length < 2) return 0; // if either is a 1-letter string
25-
const firstBigrams = new Map();
26-
const lowBigrams = new Map();
27-
for (let i = 0; i < first.length - 1; i++) {
28-
const bigram = first.substring(i, i + 2);
29-
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) + 1 : 1;
30-
const countLow = lowBigrams.has(bigram.toLowerCase()) ? lowBigrams.get(bigram.toLowerCase()) + 1 : 1;
17+
similarity: function (s2: string) {
18+
var s1 = this.toLowerCase();
19+
s2 = s2.toLowerCase();
3120

32-
lowBigrams.set(bigram.toLowerCase(), countLow);
33-
firstBigrams.set(bigram, count);
21+
if (s1.length < s2.length) {
22+
s1 = s2;
23+
s2 = this;
3424
}
35-
let intersectionSize = 0;
36-
for (let i = 0; i < second.length - 1; i++) {
37-
const bigram = second.substring(i, i + 2);
38-
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) : 0;
39-
const countLow = firstBigrams.has(bigram.toLowerCase()) ? firstBigrams.get(bigram.toLowerCase()) : 0;
40-
if (count > 0) {
41-
firstBigrams.set(bigram, count - 1);
42-
intersectionSize++;
43-
}
44-
if (countLow > 0) {
45-
firstBigrams.set(bigram.toLowerCase(), countLow - 1);
46-
intersectionSize += 0.9;
25+
var longerLength = s1.length;
26+
if (longerLength == 0) {
27+
return 1.0;
28+
}
29+
30+
var costs = new Array();
31+
for (var i = 0; i <= s1.length; i++) {
32+
var lastValue = i;
33+
for (var j = 0; j <= s2.length; j++) {
34+
if (i == 0) costs[j] = j;
35+
else {
36+
if (j > 0) {
37+
var newValue = costs[j - 1];
38+
if (s1.charAt(i - 1) != s2.charAt(j - 1))
39+
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
40+
costs[j - 1] = lastValue;
41+
lastValue = newValue;
42+
}
43+
}
4744
}
45+
if (i > 0) costs[s2.length] = lastValue;
4846
}
49-
return (2.0 * intersectionSize) / (first.length + second.length - 2);
47+
48+
return (longerLength - costs[s2.length]) / parseFloat(longerLength);
5049
},
5150
join: function (iterate: string[]) {
5251
if (typeof iterate === "string") return iterate;

0 commit comments

Comments
 (0)