🔥 Remove unused and deprecated helpers from app.util.object ns

This commit is contained in:
Andrey Antukh 2024-08-27 16:43:05 +02:00
parent 569674452a
commit 25e9129a8e
8 changed files with 63 additions and 149 deletions

View file

@ -6,12 +6,7 @@
(ns app.util.object
"A collection of helpers for work with javascript objects."
(:refer-clojure :exclude [set! new get get-in merge clone contains? array? into-array])
(:require
[cuerdas.core :as str]
;; FIXME: we use goog.string here for performance reasons, pending
;; to apply this optimizations directly to cuerdas.
[goog.string :as gstr]))
(:refer-clojure :exclude [set! new get merge clone contains? array? into-array]))
(defn array?
[o]
@ -36,24 +31,6 @@
(when (some? obj)
(js/Object.hasOwn obj k)))
(defn get-keys
[obj]
(js/Object.keys ^js obj))
(defn get-in
([obj keys]
(get-in obj keys nil))
([obj keys default]
(loop [key (first keys)
keys (rest keys)
res obj]
(if (or (nil? key) (nil? res))
(or res default)
(recur (first keys)
(rest keys)
(unchecked-get res key))))))
(defn clone
[a]
(js/Object.assign #js {} a))
@ -80,86 +57,20 @@
(js-delete obj key)
obj)
(def ^:private not-found-sym (js/Symbol "not-found"))
(defn update!
[obj key f & args]
(let [found (get obj key ::not-found)]
(if-not (identical? ::not-found found)
(do (unchecked-set obj key (apply f found args))
obj)
obj)))
(defn- props-key-fn
[k]
(if (or (keyword? k) (symbol? k))
(let [nword (name k)]
(cond
(= nword "class") "className"
(str/starts-with? nword "--") nword
(str/starts-with? nword "data-") nword
(str/starts-with? nword "aria-") nword
:else (str/camel nword)))
k))
(defn clj->props
[props]
(clj->js props :keyword-fn props-key-fn))
(let [found (get obj key not-found-sym)]
(when-not ^boolean (identical? found not-found-sym)
(unchecked-set obj key (apply f found args)))
obj))
(defn ^boolean in?
[obj prop]
(js* "~{} in ~{}" prop obj))
(defn- transform-prop-key
[s]
(let [result (js* "~{}.replace(\":\", \"-\").replace(/-./g, x=>x[1].toUpperCase())", s)]
(if ^boolean (gstr/startsWith s "-")
(gstr/capitalize result)
result)))
(defn prop-key-fn
[k]
(when (string? k)
(cond
(or (= k "class")
(= k "class-name"))
"className"
(gstr/startsWith k "data-")
k
:else
(transform-prop-key k))))
;; FIXME: REPEATED from app.common.json
(defn map->obj
"A simplified version of clj->js with focus on performance"
([x] (map->obj x identity))
([x ^function key-fn]
(cond
(nil? x)
nil
(keyword? x)
(name x)
(map? x)
(reduce-kv (fn [m k v]
(let [k (if (keyword? k) (name k) k)]
(unchecked-set m (key-fn k) (map->obj v key-fn))
m))
#js {}
x)
(coll? x)
(reduce (fn [arr v]
(.push arr v)
arr)
(array)
x)
:else x)))
(defn clear-empty
(defn without-empty
[^js obj]
(when (some? obj)
(js* "Object.entries(~{}).reduce((a, [k,v]) => (v == null ? a : (a[k]=v, a)), {}) " obj)))