Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.
This repository was archived by the owner on May 18, 2021. It is now read-only.

Multi selectors, id and class shortcuts, underscores as hyphens #3

Description

@jussiry

Started using CCSS in a CoffeeScript framework i'm building (https://github.com/jussiry/houCe), and added some new features to it:

# multiselectors:
'div, li':  ...  ->  div:  ...
                     li:   ...
div_and_li: ...  ->  div:  ...
                     li:   ...

# i_ (id) and c_ (class) prefixes to avoid typing strings:
i_name:  ...   ->   '#name':    ...
c_name:  ...   ->   '.name':    ...

# underscores as hyphens, as suggested in the other issue:
font_size: ...   ->   'font-size':  ...

And here's the iterator that adds these features:

ccss_extras = (obj)->
  for orig_key, val of obj
    ccss_extras val if typeof val is 'object'
    # split multi definitions:
    keys = orig_key.split(/,|_and_/).map('trim')
    keys.each (k)->
      # change i_plaa to '#plaa' and c_plaa to '.plaa'
      if      k[0..1] is 'i_' then k = '#'+k[2..-1]
      else if k[0..1] is 'c_' then k = '.'+k[2..-1]
      # font_size to font-size
      if typeof val isnt 'object'
        k = k.replace(/_/g,'-')
      # set new key and delete old:
      if k isnt orig_key
        if typeof val is 'object'
          obj[k] ?= {}
          obj[k].merge val
        else
          obj[k] = val
        delete obj[orig_key]
  obj

Notice that the code uses merge function from Sugar.js. You can replace that with extend from JQuery or Underscore, if those are the libraries of your choice.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions