Skip to content

Commit 0ed9398

Browse files
author
dnolen
committed
CLJS-1710: spec/double-in not implemented
add float?, double?, infinite? predicates add double-in spec :exclude float from cljs.pprint ns
1 parent 8399062 commit 0ed9398

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,22 @@ reduces them without incurring seq initialization"
21852185

21862186
:else false))
21872187

2188+
(defn ^boolean float?
2189+
"Returns true for JavaScript numbers, false otherwise."
2190+
[x]
2191+
(number? x))
2192+
2193+
(defn ^boolean double?
2194+
"Returns true for JavaScript numbers, false otherwise."
2195+
[x]
2196+
(number? x))
2197+
2198+
(defn ^boolean infinite?
2199+
"Returns true for Infinity and -Infinity values."
2200+
[x]
2201+
(or (identical? x js/Number.POSITIVE_INFINITY)
2202+
(identical? x js/Number.NEGATIVE_INFINITY)))
2203+
21882204
(defn ^boolean contains?
21892205
"Returns true if key is present in the given collection, otherwise
21902206
returns false. Note that for numerically indexed collections like

src/main/cljs/cljs/pprint.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
; You must not remove this notice, or any other, from this software.
88

99
(ns cljs.pprint
10-
(:refer-clojure :exclude [deftype print println pr prn])
10+
(:refer-clojure :exclude [deftype print println pr prn float?])
1111
(:require-macros
1212
[cljs.pprint :as m :refer [with-pretty-writer getf setf deftype
1313
pprint-logical-block print-length-loop

src/main/cljs/cljs/spec.cljc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,23 @@
443443
`(spec (and c/int? #(int-in-range? ~start ~end %))
444444
:gen #(gen/large-integer* {:min ~start :max (dec ~end)})))
445445

446+
(defmacro double-in
447+
"Specs a 64-bit floating point number. Options:
448+
449+
:infinite? - whether +/- infinity allowed (default true)
450+
:NaN? - whether NaN allowed (default true)
451+
:min - minimum value (inclusive, default none)
452+
:max - maximum value (inclusive, default none)"
453+
[& {:keys [infinite? NaN? min max]
454+
:or {infinite? true NaN? true}
455+
:as m}]
456+
`(spec (and c/double?
457+
~@(when-not infinite? '[#(not (infinite? %))])
458+
~@(when-not NaN? '[#(not (js/isNaN %))])
459+
~@(when max `[#(<= % ~max)])
460+
~@(when min `[#(<= ~min %)]))
461+
:gen #(gen/double* ~m)))
462+
446463
(defmacro merge
447464
"Takes map-validating specs (e.g. 'keys' specs) and
448465
returns a spec that returns a conformed map satisfying all of the

src/main/cljs/cljs/spec/impl/gen.cljs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ gen-builtins
9494
pos-int? (large-integer* {:min 1})
9595
neg-int? (large-integer* {:max -1})
9696
nat-int? (large-integer* {:min 0})
97-
;float? (double)
97+
float? (double)
98+
double? (double)
9899
string? (string-alphanumeric)
99100
ident? (one-of [(keyword-ns) (symbol-ns)])
100101
simple-ident? (one-of [(keyword) (symbol)])

0 commit comments

Comments
 (0)