Skip to content
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
85 changes: 82 additions & 3 deletions src/Date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,48 @@ define(Date, {
});

define(Date.prototype, {
setTimezone: function (timezone: string) {
setTimezone: function (this: Date, timezone: string) {
return new Date(
this.toLocaleString("en-US", {
timeZone: timezone,
})
);
},
isInPast: function () {
isInPast: function (this: Date) {
const today = new Date();
today.setHours(0, 0, 0, 0);
return this < today;
},
addYear: function (this: Date, years: number, month: number = 0, date: number = 0) {
return this.setFullYear(this.getFullYear() + years, this.getMonth() + month, this.getDate() + date);
},
addMonth: function (this: Date, months: number, date: number = 0) {
return this.setMonth(this.getMonth() + months, this.getDate() + date);
},
addDate: function (this: Date, days: number) {
return this.setDate(this.getDate() + days);
},
addHours: function (this: Date, hours: number, minutes: number = 0, seconds: number = 0, milliseconds: number = 0) {
return this.setHours(
this.getHours() + hours,
this.getMinutes() + minutes,
this.getSeconds() + seconds,
this.getMilliseconds() + milliseconds
);
},
addMinutes: function (this: Date, minutes: number, seconds: number = 0, milliseconds: number = 0) {
return this.setMinutes(
this.getMinutes() + minutes,
this.getSeconds() + seconds,
this.getMilliseconds() + milliseconds
);
},
addSeconds: function (this: Date, seconds: number, milliseconds: number = 0) {
return this.setSeconds(this.getSeconds() + seconds, this.getMilliseconds() + milliseconds);
},
addMilliseconds: function (this: Date, milliseconds: number) {
return this.setMilliseconds(this.getMilliseconds() + milliseconds);
},
});

declare global {
Expand All @@ -27,7 +57,7 @@ declare global {
* Returns the current timestamp as its representation in seconds
* @returns {number} date in seconds
* @example
* Date.nowSeconds() // 1671621321
* Date.nowSeconds()
*/
nowSeconds(): number;
}
Expand All @@ -51,6 +81,55 @@ declare global {
* new Date("2022-12-01").isInPast() // returns: true
*/
isInPast(): boolean;
/**
* Add a number of years to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addYear(1)
*/
addYear(years: number, month?: number, date?: number): number;
/**
* Add a number of months to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addMonth(1)
*/
addMonth(months: number, date?: number): number;
/**
* Add a number of days to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addDate(1)
*/
addDate(days: number): number;
/**
* Add a number of hours to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addHours(1)
*/
addHours(hours: number, minutes?: number, seconds?: number, milliseconds?: number): number;
/**
* Add a number of minutes to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addMinutes(1)
*/
addMinutes(minutes: number, seconds?: number, milliseconds?: number): number;
/**
* Add a number of seconds to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addSeconds(1)
*/
addSeconds(seconds: number, milliseconds?: number): number;
/**
* Add a number of milliseconds to the current date
* @returns {number} timestamp in milliseconds
* @example
* new Date().addMilliseconds(1)
*/
addMilliseconds(milliseconds: number): number;
}
}

Expand Down
57 changes: 28 additions & 29 deletions src/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,38 @@ define(String.prototype, {
replaceAll: function (find: string, replace: string) {
return this.replace(new RegExp(escapeRegExp(find), "g"), replace);
},
similarity: function (second: string) {
const first = this.replace(/\s+/g, "");
second = second.replace(/\s+/g, "");
if (!first.length && !second.length) return 1; // if both are empty strings
if (!first.length || !second.length) return 0; // if only one is empty string
if (first === second) return 1; // identical
if (first.length === 1 && second.length === 1) return 0; // both are 1-letter strings
if (first.length < 2 || second.length < 2) return 0; // if either is a 1-letter string
const firstBigrams = new Map();
const lowBigrams = new Map();
for (let i = 0; i < first.length - 1; i++) {
const bigram = first.substring(i, i + 2);
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) + 1 : 1;
const countLow = lowBigrams.has(bigram.toLowerCase()) ? lowBigrams.get(bigram.toLowerCase()) + 1 : 1;
similarity: function (s2: string) {
var s1 = this.toLowerCase();
s2 = s2.toLowerCase();

lowBigrams.set(bigram.toLowerCase(), countLow);
firstBigrams.set(bigram, count);
if (s1.length < s2.length) {
s1 = s2;
s2 = this;
}
let intersectionSize = 0;
for (let i = 0; i < second.length - 1; i++) {
const bigram = second.substring(i, i + 2);
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) : 0;
const countLow = firstBigrams.has(bigram.toLowerCase()) ? firstBigrams.get(bigram.toLowerCase()) : 0;
if (count > 0) {
firstBigrams.set(bigram, count - 1);
intersectionSize++;
}
if (countLow > 0) {
firstBigrams.set(bigram.toLowerCase(), countLow - 1);
intersectionSize += 0.9;
var longerLength = s1.length;
if (longerLength == 0) {
return 1.0;
}

var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0) costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0) costs[s2.length] = lastValue;
}
return (2.0 * intersectionSize) / (first.length + second.length - 2);

return (longerLength - costs[s2.length]) / parseFloat(longerLength);
},
join: function (iterate: string[]) {
if (typeof iterate === "string") return iterate;
Expand Down