mirror of
https://github.com/penpot/penpot.git
synced 2025-01-26 08:29:42 -05:00
🎉 Add uxbox.util.object ns.
This commit is contained in:
parent
009720696c
commit
3d4cbe28f2
1 changed files with 47 additions and 0 deletions
47
frontend/src/uxbox/util/object.cljs
Normal file
47
frontend/src/uxbox/util/object.cljs
Normal file
|
@ -0,0 +1,47 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
;; defined by the Mozilla Public License, v. 2.0.
|
||||
;;
|
||||
;; Copyright (c) 2020 UXBOX Labs SL
|
||||
|
||||
(ns uxbox.util.object
|
||||
"A collection of helpers for work with javascript objects."
|
||||
(:refer-clojure :exclude [get get-in assoc!])
|
||||
(:require [goog.object :as gobj]))
|
||||
|
||||
(defn get
|
||||
([obj k]
|
||||
(when (object? obj)
|
||||
(unchecked-get obj k)))
|
||||
([obj k default]
|
||||
(if (object? obj)
|
||||
(or (unchecked-get obj k) default)
|
||||
default)))
|
||||
|
||||
(defn get-in
|
||||
[obj keys]
|
||||
(loop [key (first keys)
|
||||
keys (rest keys)
|
||||
res obj]
|
||||
(if (nil? key)
|
||||
res
|
||||
(if (nil? res)
|
||||
res
|
||||
(recur (first keys)
|
||||
(rest keys)
|
||||
(unchecked-get res key))))))
|
||||
|
||||
(defn assign!
|
||||
([a b]
|
||||
(js/Object.assign a b))
|
||||
([a b & more]
|
||||
(reduce assign! (assign! a b) more)))
|
||||
|
||||
(defn assoc!
|
||||
[obj attr value]
|
||||
(when (object? obj)
|
||||
(unchecked-set obj attr value)
|
||||
obj))
|
Loading…
Add table
Reference in a new issue