Skip to content

Commit f400444

Browse files
authored
Merge pull request #3 from purescript-contrib/parse
Add `parse` function
2 parents 8ca0283 + 943f585 commit f400444

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/Data/JSDate.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ exports.dateMethodEff = function (method, date) {
3434
return date[method]();
3535
};
3636
};
37+
38+
exports.parse = function (dateString) {
39+
return function () {
40+
return new Date(dateString);
41+
};
42+
};

src/Data/JSDate.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ foreign import jsdateLocal
102102
foreign import dateMethodEff :: forall eff a. Fn2 String JSDate (Eff eff a)
103103
foreign import dateMethod :: forall a. Fn2 String JSDate a
104104

105+
-- | Attempts to parse a date from a string. The behaviour of this function is
106+
-- | implementation specific until ES5, so may not always have the same
107+
-- | behaviour for a given string. The RFC2822 and ISO8601 date string formats
108+
-- | should parse consistently.
109+
-- |
110+
-- | The `LOCALE` effect is present here as if no time zone is specified in the
111+
-- | string the current locale's time zone will be used instead.
112+
foreign import parse
113+
:: forall eff. String -> Eff (locale :: LOCALE | eff) JSDate
114+
105115
-- | The effect type used when indicating the current machine's date/time locale
106116
-- | is used in computing a value.
107117
foreign import data LOCALE :: !

test/Test/Main.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ main = do
3838
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { second = nan }
3939
assert $ not $ JSD.isValid $ JSD.jsdate defaultDateRecord { millisecond = nan }
4040

41+
log "Checking that a date constructed from a string with sensible values is valid..."
42+
assert <<< JSD.isValid =<< JSD.parse "2011-10-10T14:48:00"
43+
assert <<< JSD.isValid =<< JSD.parse "Thu, 01 Jan 1970 00:00:00 GMT-0400"
44+
4145
log "Checking that a local date constructed with sensible values is valid..."
4246
assert <<< JSD.isValid =<< JSD.jsdateLocal defaultDateRecord
4347

0 commit comments

Comments
 (0)