Skip to content

Commit

Permalink
Improve generated type assertion predicates to generate more obvious …
Browse files Browse the repository at this point in the history
…forms.

Before: (null? foo) => Object.prototype.toString.call(foo) === '[object Null]'
After: (null? foo) => foo === null
  • Loading branch information
Gozala committed Aug 6, 2012
1 parent 4dff3b8 commit d32f410
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/macros.ls
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,38 @@
(defmacro nil? [value]
`(== ~value 'null))
(defmacro null? [value]
`(= ~value 'null))
(defmacro undefined? [value]
`(= ~value 'undefined))
(defmacro true? [value]
`(identical? ~value 'true))
(defmacro false? [value]
`(identical? ~value 'false))
(defmacro object? [value]
`(and ~value (type-of? ~value "object")))

(defmacro type-of? [value type]
`(= (typeof ~value) ~type))

(defmacro def-typeof-predicate [name type]
`(defmacro ~name [expression]
(type-of? '(unquote expression) ~type)))
(defmacro def-type-predicate [name type]
`(defmacro ~name [expression]
(=== (.call Object.prototype.toString '(unquote expression))
(js* "'[object ~{}]'" ~type))))

(def-type-predicate object? Object)
(def-type-predicate null? Null)
(def-type-predicate undefined? Undefined)
(def-type-predicate array? Array)
(def-typeof-predicate boolean? "boolean")
(def-typeof-predicate function? "function")

(def-type-predicate string? String)
(def-type-predicate array? Array)
(def-type-predicate regexp? Regexp)
(def-type-predicate date? Date)
(def-type-predicate number? Number)
(def-type-predicate boolean? Boolean)
(def-type-predicate function? Function)
(def-type-predicate arguments? Arguments)

(defmacro do (& body)
`((fn [] ~@body)))
Expand Down

0 comments on commit d32f410

Please sign in to comment.