Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

920 pre push and commit hooks #17

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn lint-staged
4 changes: 4 additions & 0 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.{js,json}": ["eslint --fix", "prettier --write --ignore-unknown"],
"*.{css,md,html}": ["prettier --write --ignore-unknown"]
}
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
yarn.lock

# Ignore artifacts:
build
coverage
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"bracketSpacing": true,
"semi": true,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Unlike the original controls at https://github.com/OpenHistoricalMap/mbgl-timesl

For a demonstration, see **index.html** and https://openhistoricalmap.github.io/leaflet-ohm-timeslider-v2/


# Constructor Options

`position` -- The Leaflet position for the control. Defaults to `bottomright`. It is recommended to keep this, as the control has an arrow sticking off the top/left.
Expand All @@ -31,7 +30,6 @@ For a demonstration, see **index.html** and https://openhistoricalmap.github.io/

`onRangeChange: function ([date1, date2])` -- A callback function which will be called when the date range changes. The newly-available range (2-item list of ISO strings) will be passed as a param. Within the callback function, `this` will refer to the OHMTimeSlider.


# Methods

`getDate(asdecimal=false)` -- Return the currently-selected date on the slider. The date will be in ISO YYYY-MM-DD format, unless `decimaldate` is given to get it as a decimaldate number instead.
Expand Down Expand Up @@ -62,7 +60,6 @@ For a demonstration, see **index.html** and https://openhistoricalmap.github.io/

`listLanguages()` -- Return a list of the supported language translations for use with the `language` option.


# Development

Fire up a Python CLI web server via `python -m SimpleHTTPServer 9646` or `python3 -m http.server 9646`
Expand All @@ -72,12 +69,14 @@ You can now point a browser at http://localhost:9646/ and see the demo.
To create the minified JS/CSS files, use whatever tools you prefer such as UglifyJS and UglifyCSS.

Installation:

```
nvm use 16.9.0
npm install uglify-js uglifycss -g
```

Usage:

```
uglifycss --output leaflet-ohm-timeslider.min.css leaflet-ohm-timeslider.css
uglifyjs --keep-fargs --keep-fnames --output leaflet-ohm-timeslider.min.js leaflet-ohm-timeslider.js
Expand Down Expand Up @@ -130,4 +129,4 @@ We welcome PRs with new languages added to this, or if you're not sure how to do
months: ['','',...],
bce: "",
dateformat: '',
```
```
269 changes: 139 additions & 130 deletions decimaldate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,160 +6,169 @@ const decimaldate = {};

decimaldate.DECIMALPLACES = 6;

decimaldate.RE_YEARMONTHDAY = /^(\-?\+?)(\d+)\-(\d\d)\-(\d\d)$/;
decimaldate.RE_YEARMONTHDAY = /^(-?\+?)(\d+)-(\d\d)-(\d\d)$/;

decimaldate.iso2dec = (isodate) => {
// parse the date into 3 integers and maybe a minus sign
// validate that it's a valid date
const datepieces = isodate.match(decimaldate.RE_YEARMONTHDAY);
if (! datepieces) throw new Error(`iso2dec() malformed date ${isodate}`);

const [plusminus, yearstring, monthstring, daystring] = datepieces.slice(1);
const monthint = parseInt(monthstring);
const dayint = parseInt(daystring);
let yearint = plusminus == '-' ? -1 * parseInt(yearstring) : parseInt(yearstring);
if (yearint <= 0) yearint -= 1; // ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE; we want proper negative integer
if (! decimaldate.isvalidmonthday(yearint, monthint, dayint)) throw new Error(`iso2dec() invalid date ${isodate}`);

// number of days passed = decimal portion
// if BCE <=0 then count backward from the end of the year, instead of forward from January
const decbit = decimaldate.proportionofdayspassed(yearint, monthint, dayint);

let decimaloutput;
if (yearint < 0) {
// ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE; we want string version
// so it's 1 to get from the artificially-inflated integer (string 0000 => -1 for math, +1 to get back to 0)
decimaloutput = 1 + 1 + yearint - (1 - decbit);
}
else {
decimaloutput = yearint + decbit;
}

// round to standardized number of decimals
decimaloutput = parseFloat(decimaloutput.toFixed(decimaldate.DECIMALPLACES));
return decimaloutput;
// parse the date into 3 integers and maybe a minus sign
// validate that it's a valid date
const datepieces = isodate.match(decimaldate.RE_YEARMONTHDAY);
if (!datepieces) throw new Error(`iso2dec() malformed date ${isodate}`);

const [plusminus, yearstring, monthstring, daystring] = datepieces.slice(1);
const monthint = parseInt(monthstring, 10);
const dayint = parseInt(daystring, 10);
let yearint =
plusminus === "-"
? -1 * parseInt(yearstring, 10)
: parseInt(yearstring, 10);
if (yearint <= 0) yearint -= 1; // ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE; we want proper negative integer
if (!decimaldate.isvalidmonthday(yearint, monthint, dayint)) {
throw new Error(`iso2dec() invalid date ${isodate}`);
}

// number of days passed = decimal portion
// if BCE <=0 then count backward from the end of the year, instead of forward from January
const decbit = decimaldate.proportionofdayspassed(yearint, monthint, dayint);

let decimaloutput;
if (yearint < 0) {
// ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE; we want string version
// so it's 1 to get from the artificially-inflated integer (string 0000 => -1 for math, +1 to get back to 0)
decimaloutput = 1 + 1 + yearint - (1 - decbit);
} else {
decimaloutput = yearint + decbit;
}

// round to standardized number of decimals
decimaloutput = parseFloat(decimaloutput.toFixed(decimaldate.DECIMALPLACES));
return decimaloutput;
};


decimaldate.dec2iso = (decdate) => {
// remove the artificial +1 that we add to make positive dates look intuitive
const truedecdate = decdate - 1;
const ispositive = truedecdate > 0;

// get the integer year
if (ispositive) {
yearint = Math.floor(truedecdate) + 1;
}
else {
yearint = -Math.abs(Math.floor(truedecdate)); // ISO 8601 shift and year<=0 by 1, 0=1BCE, -1=2BCE
}

// how many days in year X decimal portion = number of days into the year
// if it's <0 then we count backward from the end of the year, instead of forward into the year
const dty = decimaldate.daysinyear(yearint);
let targetday = dty * (Math.abs(truedecdate) % 1);
if (ispositive) targetday = Math.ceil(targetday);
else targetday = dty - Math.floor(targetday);

// count up days months at a time, until we reach our target month
// then the remainder (days) is the day of that month
let monthint;
let dayspassed = 0;
for (let m = 1; m <= 12; m++) {
monthint = m;
const dtm = decimaldate.daysinmonth(yearint, monthint);
if (dayspassed + dtm < targetday) {
dayspassed += dtm;
}
else {
break;
}
// remove the artificial +1 that we add to make positive dates look intuitive
const truedecdate = decdate - 1;
const ispositive = truedecdate > 0;

// get the integer year. (ISO 8601 shift and year<=0 by 1, 0=1BCE, -1=2BCE)
let yearint = ispositive
? Math.floor(truedecdate) + 1
: -Math.abs(Math.floor(truedecdate));

// how many days in year X decimal portion = number of days into the year
// if it's <0 then we count backward from the end of the year, instead of forward into the year
const dty = decimaldate.daysinyear(yearint);
let targetday = dty * (Math.abs(truedecdate) % 1);
if (ispositive) targetday = Math.ceil(targetday);
else targetday = dty - Math.floor(targetday);

// count up days months at a time, until we reach our target month
// then the remainder (days) is the day of that month
let monthint;
let dayspassed = 0;
for (let m = 1; m <= 12; m++) {
monthint = m;
const dtm = decimaldate.daysinmonth(yearint, monthint);
if (dayspassed + dtm < targetday) {
dayspassed += dtm;
} else {
break;
}
const dayint = targetday - dayspassed;

// make string output
// months and day as 2 digits
// ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE
const monthstring = monthint.toString().padStart(2, '0');
const daystring = dayint.toString().padStart(2, '0');
let yearstring;
if (yearint > 0) yearstring = yearint.toString().padStart(4, '0'); // just the year as 4 digits
else if (yearint == -1) yearstring = (Math.abs(yearint + 1).toString().padStart(4, '0')); // BCE offset by 1 but do not add a - sign
else yearstring = '-' + (Math.abs(yearint + 1).toString().padStart(4, '0')); // BCE offset by 1 and add - sign

return `${yearstring}-${monthstring}-${daystring}`;
}
const dayint = targetday - dayspassed;

// make string output
// months and day as 2 digits
// ISO 8601 shift year<=0 by 1, 0=1BCE, -1=2BCE
const monthstring = monthint.toString().padStart(2, "0");
const daystring = dayint.toString().padStart(2, "0");
let yearstring;
if (yearint > 0) {
yearstring = yearint.toString().padStart(4, "0");
} // just the year as 4 digits
else if (yearint === -1) {
yearstring = Math.abs(yearint + 1)
.toString()
.padStart(4, "0");
} // BCE offset by 1 but do not add a - sign
else {
yearstring =
"-" +
Math.abs(yearint + 1)
.toString()
.padStart(4, "0");
} // BCE offset by 1 and add - sign

return `${yearstring}-${monthstring}-${daystring}`;
};


decimaldate.isvalidmonthday = (yearint, monthint, dayint) => {
if (yearint != parseInt(yearint) || yearint == 0) return false;
if (monthint != parseInt(monthint)) return false;
if (dayint != parseInt(dayint)) return false;
if (yearint !== parseInt(yearint, 10) || yearint === 0) return false;
if (monthint !== parseInt(monthint, 10)) return false;
if (dayint !== parseInt(dayint, 10)) return false;

if (monthint < 1 || monthint > 12) return false;
if (dayint < 1) return false;
if (monthint < 1 || monthint > 12) return false;
if (dayint < 1) return false;

const dtm = decimaldate.daysinmonth(yearint, monthint);
if (! dtm) return false;
if (dayint > dtm) return false;
const dtm = decimaldate.daysinmonth(yearint, monthint);
if (!dtm) return false;
if (dayint > dtm) return false;

return true;
return true;
};


decimaldate.proportionofdayspassed = (yearint, monthint, dayint) => {
// count the number of days to get through the prior months
let dayspassed = 0;
for (let m = 1; m < monthint; m++) {
const dtm = decimaldate.daysinmonth(yearint, m);
dayspassed += dtm;
}

// add the leftover days not in a prior month
// but minus 0.5 to get us to noon of the target day, as opposed to the end of the day
dayspassed = dayspassed + dayint - 0.5;

// divide by days in year, to get decimal portion
// even January 1 is 0.5 days in since we snap to 12 noon
const dty = decimaldate.daysinyear(yearint);
return dayspassed / dty;
// count the number of days to get through the prior months
let dayspassed = 0;
for (let m = 1; m < monthint; m++) {
const dtm = decimaldate.daysinmonth(yearint, m);
dayspassed += dtm;
}

// add the leftover days not in a prior month
// but minus 0.5 to get us to noon of the target day, as opposed to the end of the day
dayspassed = dayspassed + dayint - 0.5;

// divide by days in year, to get decimal portion
// even January 1 is 0.5 days in since we snap to 12 noon
const dty = decimaldate.daysinyear(yearint);
return dayspassed / dty;
};


decimaldate.daysinmonth = (yearint, monthint) => {
const monthdaycounts = {
1: 31,
2: 28, // February
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
};

if (decimaldate.isleapyear(yearint)) monthdaycounts[2] = 29;

return monthdaycounts[monthint];
const monthdaycounts = {
1: 31,
2: 28, // February
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
};

if (decimaldate.isleapyear(yearint)) monthdaycounts[2] = 29;

return monthdaycounts[monthint];
};


decimaldate.daysinyear = (yearint) => {
return decimaldate.isleapyear(yearint) ? 366 : 365;
return decimaldate.isleapyear(yearint) ? 366 : 365;
};


decimaldate.isleapyear = (yearint) => {
if (yearint != parseInt(yearint) || yearint == 0) throw new Error(`isleapyear() invalid year ${yearint}`);
if (yearint !== parseInt(yearint, 10) || yearint === 0) {
throw new Error(`isleapyear() invalid year ${yearint}`);
}

// don't forget BCE; there is no 0 so leap years are -1, -5, -9, ..., -2001, -2005, ...
// just add 1 to the year to correct for this, for this purpose
const yearnumber = yearint > 0 ? yearint : yearint + 1;
// don't forget BCE; there is no 0 so leap years are -1, -5, -9, ..., -2001, -2005, ...
// just add 1 to the year to correct for this, for this purpose
const yearnumber = yearint > 0 ? yearint : yearint + 1;

const isleap = yearnumber % 4 == 0 && (yearnumber % 100 != 0 || yearnumber % 400 == 0);
return isleap;
const isleap =
yearnumber % 4 === 0 && (yearnumber % 100 !== 0 || yearnumber % 400 === 0);
return isleap;
};
Loading