spacetime-week-start 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +21 -12
- package/builds/spacetime-week-start.cjs +1 -1
- package/builds/spacetime-week-start.min.js +1 -1
- package/builds/spacetime-week-start.mjs +1 -1
- package/package.json +28 -24
- package/src/index.js +7 -1
- package/rollup.config.js +0 -26
- package/scratch.js +0 -7
- package/src/data/countries.js +0 -267
- package/src/input/weekStart.js +0 -66
- package/test/basic.test.js +0 -58
- package/zonefile/iana.js +0 -2501
package/src/input/weekStart.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import c from '../data/countries.js'
|
|
2
|
-
import iana from '../../zonefile/iana.js'
|
|
3
|
-
const firstDay = c.firstDay()
|
|
4
|
-
const loc = c.locations()
|
|
5
|
-
|
|
6
|
-
function getCountry(country) {
|
|
7
|
-
for (const day in firstDay) {
|
|
8
|
-
if (firstDay.hasOwnProperty(day)) {
|
|
9
|
-
for (const key in firstDay[day]) {
|
|
10
|
-
if (firstDay[day].hasOwnProperty(key)) {
|
|
11
|
-
if ((key === country) || (firstDay[day][key].indexOf(country) !== -1)) {
|
|
12
|
-
return { day: day, country: firstDay[day][key] }
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
for (const item in loc) {
|
|
19
|
-
if (loc.hasOwnProperty(item) && loc[item].indexOf(country) !== -1) {
|
|
20
|
-
return { day: 'monday', location: country }
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return {}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
// find a week-start for an iana timezone name, like 'europe/berlin'
|
|
27
|
-
function getCurrent(tz) {
|
|
28
|
-
if (!tz || typeof tz !== 'string') {
|
|
29
|
-
return null
|
|
30
|
-
}
|
|
31
|
-
tz = tz.toLowerCase()
|
|
32
|
-
const zone = iana[tz]
|
|
33
|
-
if (!zone) {
|
|
34
|
-
return null
|
|
35
|
-
}
|
|
36
|
-
if (zone.loc) {
|
|
37
|
-
return getCountry(zone.loc)
|
|
38
|
-
}
|
|
39
|
-
if (zone.ctry) {
|
|
40
|
-
return getCountry(zone.ctry)
|
|
41
|
-
}
|
|
42
|
-
// otherwise, guess from the timezone name itself
|
|
43
|
-
let found = getCountry(tz.substr(0, tz.indexOf('/')))
|
|
44
|
-
if (found.day) {
|
|
45
|
-
return found
|
|
46
|
-
}
|
|
47
|
-
found = getCountry(tz.substr(tz.indexOf('/') + 1).replace('_', ' '))
|
|
48
|
-
if (found.day) {
|
|
49
|
-
return found
|
|
50
|
-
}
|
|
51
|
-
return null
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function getWeekStart(country, tz) {
|
|
55
|
-
// try a country-name lookup first
|
|
56
|
-
if (country && typeof country === 'string') {
|
|
57
|
-
const found = getCountry(country.toLowerCase().trim())
|
|
58
|
-
if (found.day) {
|
|
59
|
-
return found
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// otherwise, use the given timezone
|
|
63
|
-
return getCurrent(tz)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export { getWeekStart }
|
package/test/basic.test.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import test from 'tape'
|
|
2
|
-
import spacetime from '../../../src/index.js'
|
|
3
|
-
import weekStartPlugin from '../src/index.js'
|
|
4
|
-
|
|
5
|
-
spacetime.extend(weekStartPlugin)
|
|
6
|
-
|
|
7
|
-
test('uses the timezone of the spacetime object', t => {
|
|
8
|
-
t.deepEqual(spacetime.now('Europe/Berlin').weekStart(), { day: 'monday', country: 'germany' })
|
|
9
|
-
t.deepEqual(spacetime.now('America/Toronto').weekStart(), { day: 'sunday', country: 'canada' })
|
|
10
|
-
t.deepEqual(spacetime.now('Asia/Tehran').weekStart(), { day: 'saturday', country: 'iran' })
|
|
11
|
-
t.end()
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
test('general timezones return a location', t => {
|
|
15
|
-
t.deepEqual(spacetime.now('Etc/Zulu').weekStart(), { day: 'monday', location: 'zulu' })
|
|
16
|
-
t.end()
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
test('falsy or unknown input falls back to the timezone', t => {
|
|
20
|
-
const s = spacetime.now('America/Toronto')
|
|
21
|
-
t.deepEqual(s.weekStart(null), { day: 'sunday', country: 'canada' })
|
|
22
|
-
t.deepEqual(s.weekStart(''), { day: 'sunday', country: 'canada' })
|
|
23
|
-
t.deepEqual(s.weekStart(12), { day: 'sunday', country: 'canada' })
|
|
24
|
-
t.deepEqual(s.weekStart('xyzxyz'), { day: 'sunday', country: 'canada' })
|
|
25
|
-
t.end()
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
test('a country name wins over the timezone', t => {
|
|
29
|
-
const s = spacetime.now('Europe/Berlin')
|
|
30
|
-
t.equal(s.weekStart('canada').day, 'sunday')
|
|
31
|
-
t.equal(s.weekStart('canada').country, 'canada')
|
|
32
|
-
t.equal(s.weekStart('iran').day, 'saturday')
|
|
33
|
-
t.end()
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('partial country names match', t => {
|
|
37
|
-
const s = spacetime.now('America/Toronto')
|
|
38
|
-
t.equal(s.weekStart('united arab emirates').day, 'sunday')
|
|
39
|
-
t.equal(s.weekStart('emirates').country, 'united arab emirates')
|
|
40
|
-
t.end()
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test('country name can be in lower case, upper case or camel case', t => {
|
|
44
|
-
const s = spacetime.now('America/Toronto')
|
|
45
|
-
t.equal(s.weekStart('cana').day, 'sunday')
|
|
46
|
-
t.equal(s.weekStart('nada').country, 'grenada')
|
|
47
|
-
t.notEqual(s.weekStart('nada').country, 'canada')
|
|
48
|
-
// finds first occourance of string 'nada' in JSON
|
|
49
|
-
t.equal(s.weekStart('CANADA').day, 'sunday')
|
|
50
|
-
t.equal(s.weekStart('Canada').country, 'canada')
|
|
51
|
-
// it's located in array under key "monday" for
|
|
52
|
-
// grenada and after that appears located in
|
|
53
|
-
// canada under key "sunday"
|
|
54
|
-
t.equal(s.weekStart('CaNa').day, 'sunday')
|
|
55
|
-
t.equal(s.weekStart('nAdA').country, 'grenada')
|
|
56
|
-
t.notEqual(s.weekStart('nAdA').country, 'canada')
|
|
57
|
-
t.end()
|
|
58
|
-
})
|