Giter Club home page Giter Club logo

wharf's Introduction

Wharf

/(h)wôrf/ A level area to which a ship may be moored to load and unload.

Wharf is a Clojure library for transforming map keys. It uses clojure.walk/postwalk and comes bundled with many key transformers. The use case that pushed Wharf into existence is to be able to subscribe to and publish JSON APIs with out littering your Clojure code with underscores and camel case variables.

Usage

The following examples assume wharf.core is required.

(require '[wharf.core :refer :all])

The main function is transform-keys, which will walk the provided collection and trasnform all the keys using the provided function.

(transform-keys clojure.string/upper-case {"foo" "bar"})
;=> {"FOO" "bar"}

The function you pass in can be any function that accepts one argument. The example below uses the keyword function.

(transform-keys keyword {"foo" "bar" "goo" "gar"})
;=> {:foo "bar", :goo "gar"}

One common idiom is to compose functions.

(transform-keys (comp keyword uncapitalize) {"Foo" "bar"})
;=> {:foo "bar"}

There are also some case-conversion functions available.

(transform-keys camel->hyphen {"FooBar" "baz"})
;=> {"foo-bar" "baz"}

Here is how you would convert a camel-case string to a dash-case keyword and vice versa.

(transform-keys (comp keyword clojure.string/lower-case camel->hyphen) {"FooBar" "baz"})
;=> {:foo-bar "baz"}

(transform-keys (comp hyphen->upper-camel name) {:foo-bar "baz"})
;=> {"FooBar" "baz"}

A complex example.

(transform-keys (comp keyword clojure.string/lower-case camel->hyphen)
                {"FooBar" [{"Fancy1" nil "R2D2" nil} {"MoreNo1se" nil}]})
;=> {:foo-bar [{:fancy1 nil, :r2-d2 nil} {:more-no1se nil}]}

License

Copyright © 2014 Jeremy Heiler

Distributed under the Eclipse Public License, the same as Clojure.

wharf's People

Contributors

jeremyheiler avatar kisom avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

wharf's Issues

Split conversion functions

It might be useful to split the conversion functions so that its easier to compose them and map over their results.

Consume all but the last capital letter as a single token

Current:

(clojure.string/lower-case (w/camel->dash "SQLFoo"))
;=> "s-q-l-foo"

Want:

(clojure.string/lower-case (w/camel->dash "SQLFoo"))
;=> "sql-foo"

This must remain the same:

(clojure.string/lower-case (w/camel->dash "IFoo"))
;=> "i-foo"

How to deal with sequential capitals?

Given a string "IMEINumber", I want to get out "imei-number", but camel->dash expects title case:

(camel->dash "IMEINumber")
=> "I-M-E-I-Number"

Which is expected behaviour, as per the tests. Is there a trick to transform the sequential "IMEINumber" to "ImeiNumber" to get the desired output?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.