Skip to content

Instantly share code, notes, and snippets.

@jaen

jaen/alternative.cljs Secret

Created Dec 29, 2015
Embed
What would you like to do?
(def confidence-threshold 90)
(def target-charset "UTF-8")
(defn open-file [path]
(let [content (.readFileSync fs path)]
{:path path :content content}))
(defn detect-charset [{:keys [content] :as file-descriptor}]
(let [charset (.detectCharset charsetDetector content)]
(merge file-descriptor
{:charset (.toString charset)
:confidence (.-confidence charset)})))
(defn is-confident? [{:keys [path confidence] :as file-descriptor}]
(if (> confidence confidence-threshold)
file-descriptor
(do
(println (str "The charset confidence of " path " is " confidence
", which is below the threshold of " confidence-threshold "."))
nil)))
(defn convert-to-utf8 [{:keys [content charset] :as file-descriptor}]
(if (not= target-charset charset)
(let [converted-content (cr (.decode iconv content charset))]
(merge file-descriptor {:converted-content converted-content
:converted? true}))
file-descriptor))
(defn print-error [{:keys [error]}]
(println "There was an error: " error))
(defn write-file! [{:keys [path decoded-content]}]
(println "I would write the converted file out here: " path))
(defn clean-file! [path]
(let [result (some-> path
open-file
detect-charset
is-confident?
convert-to-utf8)]
(when result
(write-file! result))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment