Skip to content

Commit 62930a7

Browse files
committed
Initial commit
0 parents  commit 62930a7

File tree

11 files changed

+456
-0
lines changed

11 files changed

+456
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.*
2+
!/.gitignore
3+
!/.jscsrc
4+
!/.jshintrc
5+
!/.travis.yml
6+
/bower_components/
7+
/node_modules/
8+
/output/

.jscsrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"preset": "grunt",
3+
"disallowSpacesInFunctionExpression": null,
4+
"requireSpacesInFunctionExpression": {
5+
"beforeOpeningRoundBrace": true,
6+
"beforeOpeningCurlyBrace": true
7+
},
8+
"disallowSpacesInAnonymousFunctionExpression": null,
9+
"requireSpacesInAnonymousFunctionExpression": {
10+
"beforeOpeningRoundBrace": true,
11+
"beforeOpeningCurlyBrace": true
12+
},
13+
"disallowSpacesInsideObjectBrackets": null,
14+
"requireSpacesInsideObjectBrackets": "all",
15+
"validateQuoteMarks": "\"",
16+
"requireCurlyBraces": null
17+
}

.jshintrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"bitwise": true,
3+
"eqeqeq": true,
4+
"forin": true,
5+
"freeze": true,
6+
"funcscope": true,
7+
"futurehostile": true,
8+
"strict": "global",
9+
"latedef": true,
10+
"noarg": true,
11+
"nocomma": true,
12+
"nonew": true,
13+
"notypeof": true,
14+
"singleGroups": true,
15+
"undef": true,
16+
"unused": true,
17+
"eqnull": true,
18+
"predef": ["exports"]
19+
}

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
node_js: 6
5+
env:
6+
- PATH=$HOME/purescript:$PATH
7+
install:
8+
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
9+
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
10+
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
11+
- chmod a+x $HOME/purescript
12+
- npm install -g bower
13+
- npm install
14+
- bower install
15+
script:
16+
- npm run -s build
17+
after_success:
18+
- >-
19+
test $TRAVIS_TAG &&
20+
echo $GITHUB_TOKEN | pulp login &&
21+
echo y | pulp publish --no-push

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 PureScript
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# purescript-js-date
2+
3+
[![Latest release](http://img.shields.io/bower/v/purescript-js-date.svg)](https://github.com/purescript-contrib/purescript-js-date/releases)
4+
[![Build Status](https://travis-ci.org/purescript-contrib/purescript-js-date.svg?branch=master)](https://travis-ci.org/purescript-contrib/purescript-js-date)
5+
[![Maintainer: garyb](https://img.shields.io/badge/maintainer-garyb-lightgrey.svg)](http://github.com/garyb)
6+
7+
JavaScript's native date type and corresponding functions.
8+
9+
## Installation
10+
11+
```
12+
bower install purescript-js-date
13+
```
14+
15+
## Documentation
16+
17+
This library provides the `JSDate` type and associated functions for interop purposes with JavaScript, but for working with dates in PureScript it is recommended that `DateTime` representation is used - `DateTime` offers greater type safety, a more PureScript-friendly interface, and has a `Generic` instance. There is a `toDateTime` provided for this conversion.
18+
19+
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-js-date).

bower.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "purescript-js-date",
3+
"homepage": "https://github.com/purescript-contrib/purescript-js-date",
4+
"description": "PureScript library for native JavaScript `Date` values",
5+
"license": "MIT",
6+
"repository": {
7+
"type": "git",
8+
"url": "git://github.com/purescript-contrib/purescript-js-date.git"
9+
},
10+
"ignore": [
11+
"**/.*",
12+
"bower_components",
13+
"node_modules",
14+
"output",
15+
"bower.json",
16+
"package.json"
17+
],
18+
"dependencies": {
19+
"purescript-datetime": "^1.0.0",
20+
"purescript-exceptions": "^1.0.0",
21+
"purescript-integers": "^1.0.0"
22+
},
23+
"devDependencies": {
24+
"purescript-assert": "^1.0.0",
25+
"purescript-console": "^1.0.0",
26+
"purescript-globals": "^1.0.0"
27+
}
28+
}

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"clean": "rimraf output && rimraf .pulp-cache",
5+
"build": "jshint src && jscs src && pulp build --censor-lib --strict",
6+
"test": "pulp test"
7+
},
8+
"devDependencies": {
9+
"jscs": "^2.8.0",
10+
"jshint": "^2.9.1",
11+
"pulp": "^9.0.0",
12+
"purescript-psa": "^0.3.8",
13+
"rimraf": "^2.5.0"
14+
}
15+
}

src/Data/JSDate.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* global exports */
2+
"use strict";
3+
4+
exports.isValid = function (date) {
5+
return !isNaN(date.getTime());
6+
};
7+
8+
exports.toInstantImpl = function (just) {
9+
return function (nothing) {
10+
return function (date) {
11+
var t = date.getTime();
12+
return isNaN(t) ? nothing : just(t);
13+
};
14+
};
15+
};
16+
17+
exports.jsdate = function (parts) {
18+
var t = Date.UTC(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
19+
return new Date(t);
20+
};
21+
22+
exports.jsdateLocal = function (parts) {
23+
return function () {
24+
return new Date(parts.year, parts.month, parts.day, parts.hour, parts.minute, parts.second, parts.millisecond);
25+
};
26+
};
27+
28+
exports.dateMethod = function (method, date) {
29+
return date[method]();
30+
};
31+
32+
exports.dateMethodEff = function (method, date) {
33+
return function () {
34+
return date[method]();
35+
};
36+
};

src/Data/JSDate.purs

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
-- | A module providing a type and operations for the native JavaScript `Date`
2+
-- | object.
3+
-- |
4+
-- | The `JSDate` type and associated functions are provided for interop
5+
-- | purposes with JavaScript, but for working with dates in PureScript it is
6+
-- | recommended that `DateTime` representation is used instead - `DateTime`
7+
-- | offers greater type safety, a more PureScript-friendly interface, and has
8+
-- | a `Generic` instance.
9+
module Data.JSDate where
10+
11+
import Prelude
12+
13+
import Control.Monad.Eff (Eff)
14+
import Control.Monad.Eff.Exception (EXCEPTION)
15+
16+
import Data.Date as Date
17+
import Data.DateTime (DateTime(..), Date)
18+
import Data.DateTime as DateTime
19+
import Data.DateTime.Instant (Instant)
20+
import Data.DateTime.Instant as Instant
21+
import Data.Enum (fromEnum)
22+
import Data.Function.Uncurried (Fn2, runFn2)
23+
import Data.Int (toNumber)
24+
import Data.Maybe (Maybe(..))
25+
import Data.Time as Time
26+
import Data.Time.Duration (Milliseconds(..))
27+
28+
-- | The type of JavaScript `Date` objects.
29+
foreign import data JSDate :: *
30+
31+
-- | Checks whether a date value is valid. When a date is invalid, the majority
32+
-- | of the functions return `NaN`, `"Invalid Date"`, or throw an exception.
33+
foreign import isValid :: JSDate -> Boolean
34+
35+
-- | Converts a `DateTime` value into a native JavaScript date object. The
36+
-- | resulting date value is guaranteed to be valid.
37+
fromDateTime :: DateTime -> JSDate
38+
fromDateTime (DateTime d t) = jsdate
39+
{ year: toNumber (fromEnum (Date.year d))
40+
, month: toNumber (fromEnum (Date.month d) - 1)
41+
, day: toNumber (fromEnum (Date.day d))
42+
, hour: toNumber (fromEnum (Time.hour t))
43+
, minute: toNumber (fromEnum (Time.minute t))
44+
, second: toNumber (fromEnum (Time.second t))
45+
, millisecond: toNumber (fromEnum (Time.millisecond t))
46+
}
47+
48+
-- | Attempts to construct a `DateTime` value for a `JSDate`. `Nothing` is
49+
-- | returned only when the date value is an invalid date.
50+
toDateTime :: JSDate -> Maybe DateTime
51+
toDateTime = map Instant.toDateTime <$> toInstant
52+
53+
-- | Attempts to construct a `Date` value for a `JSDate`, ignoring any time
54+
-- | component of the `JSDate`. `Nothing` is returned only when the date value
55+
-- | is an invalid date.
56+
toDate :: JSDate -> Maybe Date
57+
toDate = map DateTime.date <$> toDateTime
58+
59+
-- | Attempts to construct an `Instant` for a `JSDate`. `Nothing` is returned
60+
-- | only when the date value is an invalid date.
61+
toInstant :: JSDate -> Maybe Instant
62+
toInstant = Instant.instant <<< Milliseconds <=< toInstantImpl Just Nothing
63+
64+
foreign import toInstantImpl
65+
:: (forall a. a -> Maybe a)
66+
-> (forall a. Maybe a)
67+
-> JSDate
68+
-> Maybe Number
69+
70+
-- | Constructs a new `JSDate` from UTC component values. If any of the values
71+
-- | are `NaN` the resulting date will be invalid.
72+
foreign import jsdate
73+
:: { year :: Number
74+
, month :: Number
75+
, day :: Number
76+
, hour :: Number
77+
, minute :: Number
78+
, second :: Number
79+
, millisecond :: Number
80+
}
81+
-> JSDate
82+
83+
-- | Constructs a new `JSDate` from component values using the current machine's
84+
-- | locale. If any of the values are `NaN` the resulting date will be invalid.
85+
foreign import jsdateLocal
86+
:: forall eff
87+
. { year :: Number
88+
, month :: Number
89+
, day :: Number
90+
, hour :: Number
91+
, minute :: Number
92+
, second :: Number
93+
, millisecond :: Number
94+
}
95+
-> Eff (locale :: LOCALE | eff) JSDate
96+
97+
foreign import dateMethodEff :: forall eff a. Fn2 String JSDate (Eff eff a)
98+
foreign import dateMethod :: forall a. Fn2 String JSDate a
99+
100+
-- | The effect type used when indicating the current machine's date/time locale
101+
-- | is used in computing a value.
102+
foreign import data LOCALE :: !
103+
104+
-- | Returns the date as a number of milliseconds since 1970-01-01 00:00:00 UTC.
105+
getTime :: JSDate -> Number
106+
getTime dt = runFn2 dateMethod "getTime" dt
107+
108+
-- | Returns the day of the month for a date, according to UTC.
109+
getUTCDate :: JSDate -> Number
110+
getUTCDate dt = runFn2 dateMethod "getUTCDate" dt
111+
112+
-- | Returns the day of the week for a date, according to UTC.
113+
getUTCDay :: JSDate -> Number
114+
getUTCDay dt = runFn2 dateMethod "getUTCDay" dt
115+
116+
-- | Returns the year for a date, according to UTC.
117+
getUTCFullYear :: JSDate -> Number
118+
getUTCFullYear dt = runFn2 dateMethod "getUTCFullYear" dt
119+
120+
-- | Returns the hours for a date, according to UTC.
121+
getUTCHours :: JSDate -> Number
122+
getUTCHours dt = runFn2 dateMethod "getUTCHours" dt
123+
124+
-- | Returns the milliseconds for a date, according to UTC.
125+
getUTCMilliseconds :: JSDate -> Number
126+
getUTCMilliseconds dt = runFn2 dateMethod "getUTCMilliseconds" dt
127+
128+
-- | Returns the minutes for a date, according to UTC.
129+
getUTCMinutes :: JSDate -> Number
130+
getUTCMinutes dt = runFn2 dateMethod "getUTCMinutes" dt
131+
132+
-- | Returns the month for a date, according to UTC.
133+
getUTCMonth :: JSDate -> Number
134+
getUTCMonth dt = runFn2 dateMethod "getUTCMonth" dt
135+
136+
-- | Returns the seconds for a date, according to UTC.
137+
getUTCSeconds :: JSDate -> Number
138+
getUTCSeconds dt = runFn2 dateMethod "getUTCSeconds" dt
139+
140+
-- | Returns the day of the month for a date, according to the current
141+
-- | machine's date/time locale.
142+
getDate :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
143+
getDate dt = runFn2 dateMethodEff "getDate" dt
144+
145+
-- | Returns the day of the week for a date, according to the current
146+
-- | machine's date/time locale.
147+
getDay :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
148+
getDay dt = runFn2 dateMethodEff "getDay" dt
149+
150+
-- | Returns the year for a date, according to the current machine's date/time
151+
-- | locale.
152+
getFullYear :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
153+
getFullYear dt = runFn2 dateMethodEff "getFullYear" dt
154+
155+
-- | Returns the hour for a date, according to the current machine's date/time
156+
-- | locale.
157+
getHours :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
158+
getHours dt = runFn2 dateMethodEff "getHours" dt
159+
160+
-- | Returns the milliseconds for a date, according to the current machine's
161+
-- | date/time locale.
162+
getMilliseconds :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
163+
getMilliseconds dt = runFn2 dateMethodEff "getMilliseconds" dt
164+
165+
-- | Returns the minutes for a date, according to the current machine's
166+
-- | date/time locale.
167+
getMinutes :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
168+
getMinutes dt = runFn2 dateMethodEff "getMinutes" dt
169+
170+
-- | Returns the month for a date, according to the current machine's
171+
-- | date/time locale.
172+
getMonth :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
173+
getMonth dt = runFn2 dateMethodEff "getMonth" dt
174+
175+
-- | Returns the seconds for a date, according to the current machine's
176+
-- | date/time locale.
177+
getSeconds :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
178+
getSeconds dt = runFn2 dateMethodEff "getSeconds" dt
179+
180+
-- | Returns the time-zone offset for a date, according to the current machine's
181+
-- | date/time locale.
182+
getTimezoneOffset :: forall eff. JSDate -> Eff (locale :: LOCALE | eff) Number
183+
getTimezoneOffset dt = runFn2 dateMethodEff "getTimezoneOffset" dt
184+
185+
-- | Returns the date portion of a date value as a human-readable string.
186+
toDateString :: JSDate -> String
187+
toDateString dt = runFn2 dateMethod "toDateString" dt
188+
189+
-- | Converts a date value to an ISO 8601 Extended format date string.
190+
toISOString :: forall eff. JSDate -> Eff (exception :: EXCEPTION | eff) String
191+
toISOString dt = runFn2 dateMethodEff "toISOString" dt
192+
193+
-- | Returns a string representing for a date value.
194+
toString :: JSDate -> String
195+
toString dt = runFn2 dateMethod "toString" dt
196+
197+
-- | Returns the time portion of a date value as a human-readable string.
198+
toTimeString :: JSDate -> String
199+
toTimeString dt = runFn2 dateMethod "toTimeString" dt
200+
201+
-- | Returns the date as a string using the UTC timezone.
202+
toUTCString :: JSDate -> String
203+
toUTCString dt = runFn2 dateMethod "toUTCString" dt

0 commit comments

Comments
 (0)