commit
message
[README.md, WEEKNUM] Cleaning up TODOs and WEEKNUM formula
author
Ben Vogt <[email protected]>
date
2017-05-01 22:54:09
stats
2 file(s) changed,
26 insertions(+),
53 deletions(-)
files
README.md
src/Formulas/Date.ts
1diff --git a/README.md b/README.md
2index 609e2e9..ba89d27 100644
3--- a/README.md
4+++ b/README.md
5@@ -10,8 +10,8 @@ And the same for MAX, MAXA, COUNT, COUNTA, etc. Look these over.
6 ### Criteria evaluations should escape reg-ex characters
7 http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
8
9-### jStat functions should know their caller
10-Either through `arguments`, or directly passed in like `mean("FORMULA", [10, 20, 30])`
11+### functions that throw errors should usually be able to include their caller in the error
12+e.g. "SUM expects number values...", or "This function expects number values..."
13
14 ### Dollar functions have special types
15 Although dollar functions look like they just format `number`s, it seems like they have a special type under the hood.
16@@ -39,18 +39,10 @@ Right now we're just using the number of days since 1900, but we should check th
17
18 ### Verify that all N-times ({2,9}) are correct, and we're not parsing numbers too big.
19
20-### Scrape jsdocs from functions, put in simple index.html, doc.md files to serve up simple documentation
21+### Scrape jsdocs for functions, put in simple index.html, doc.md files to serve up simple documentation
22
23 ### Numbers with commas in them should still parse to numbers.
24
25 ### Ensure all formulas are tested inside of SheetFormulaTest.ts
26
27-
28-## Testing Guidelines
29-
30-All formulas should test for:
31-1) One *less* argument than the formula expects, and one *more* argument than the formula expects.
32-2) If it accepts a number, test with false as 0, and true as 1.
33-3) If it accepts a number, test with string parsing to number.
34-4) If it accepts a date, test with number, number as string, date as string.
35-5) Individual arguments as ranges with single values (eg: `[1]`), and ranges as multiple values (eg: `[1, "str"]`).
36+### Test all functions in src/Utilities
37diff --git a/src/Formulas/Date.ts b/src/Formulas/Date.ts
38index 684fa43..b37cd9b 100644
39--- a/src/Formulas/Date.ts
40+++ b/src/Formulas/Date.ts
41@@ -263,6 +263,23 @@ var WEEKDAY = function (...values) {
42 };
43
44
45+/**
46+ * Given a moment, an array of days of the week for shifting, will calculate the week number.
47+ * @param dm moment to iterate towards
48+ * @param shifterArray array of numbers for mapping week days to shifted weekdays
49+ * @returns {number} of weeks in year
50+ */
51+function calculateWeekNum(dm : moment.Moment, shifterArray : Array<number>) : number {
52+ var startOfYear = moment.utc(dm).startOf("year");
53+ var weeksCount = 1;
54+ var d = moment.utc(dm).startOf("year").add(6 - shifterArray[startOfYear.day()], "days");
55+ while (d.isBefore(dm)) {
56+ d.add(7, "days");
57+ weeksCount++;
58+ }
59+ return weeksCount;
60+}
61+
62 /**
63 * Returns a number representing the week of the year where the provided date falls. When inputting the date, it is best
64 * to use the DATE function, as text values may return errors.
65@@ -278,7 +295,6 @@ var WEEKDAY = function (...values) {
66 * the system used for determining the first week of the year (1=Sunday, 2=Monday).
67 * @returns {number} representing week number of year.
68 * @constructor
69- * TODO: The performance of this function could be improved.
70 */
71 var WEEKNUM = function (...values) {
72 ArgsChecker.checkLengthWithin(values, 1, 2);
73@@ -322,44 +338,13 @@ var WEEKNUM = function (...values) {
74 }
75 return week;
76 } else if (shiftType === 14) {
77- var SHIFTER = [3, 4, 5, 6, 0, 1, 2];
78- var startOfYear = moment.utc(dm).startOf("year");
79- var weeksCount = 1;
80- var d = moment.utc(dm).startOf("year").add(6 - SHIFTER[startOfYear.day()], "days");
81- while (d.isBefore(dm)) {
82- d.add(7, "days");
83- weeksCount++;
84- }
85- return weeksCount;
86+ return calculateWeekNum(dm, [3, 4, 5, 6, 0, 1, 2]);
87 } else if (shiftType === 15) {
88- var SHIFTER = [2, 3, 4, 5, 6, 0, 1];
89- var startOfYear = moment.utc(dm).startOf("year");
90- var weeksCount = 1;
91- var d = moment.utc(dm).startOf("year").add(6 - SHIFTER[startOfYear.day()], "days");
92- while (d.isBefore(dm)) {
93- d.add(7, "days");
94- weeksCount++;
95- }
96- return weeksCount;
97+ return calculateWeekNum(dm, [2, 3, 4, 5, 6, 0, 1]);
98 } else if (shiftType === 16) {
99- var SHIFTER = [1, 2, 3, 4, 5, 6, 0];
100- var startOfYear = moment.utc(dm).startOf("year");
101- var weeksCount = 1;
102- var d = moment.utc(dm).startOf("year").add(6 - SHIFTER[startOfYear.day()], "days");
103- while (d.isBefore(dm)) {
104- d.add(7, "days");
105- weeksCount++;
106- }
107- return weeksCount;
108+ return calculateWeekNum(dm, [1, 2, 3, 4, 5, 6, 0]);
109 } else if (shiftType === 17) {
110- var startOfYear = moment.utc(dm).startOf("year");
111- var weeksCount = 1;
112- var d = moment.utc(dm).startOf("year").add(6 - startOfYear.day(), "days");
113- while (d.isBefore(dm)) {
114- d.add(7, "days");
115- weeksCount++;
116- }
117- return weeksCount;
118+ return calculateWeekNum(dm, [0, 1, 2, 3, 4, 5, 6]);
119 } else if (shiftType === 21) {
120 return dm.isoWeek();
121 } else {
122@@ -420,10 +405,6 @@ var DATEDIF = function (...values) : number {
123 var months = Math.floor(e.diff(s, "months"));
124 return months === 12 ? 0 : months;
125 } else if (unitClean === "YD") {
126- // var s = start.toMoment();
127- // var e = end.toMoment();
128- // var days = e.diff(s, "days");
129- // return days
130 var s = start.toMoment();
131 var e = end.toMoment();
132 while(s.isBefore(e)) {