Coder Social home page Coder Social logo

arccoza / zenhand Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 71 KB

Turn a shorthand CSS selector like HTML string into an HTML object.

License: MIT License

JavaScript 98.89% Shell 1.11%
zen coding zenhand shorthand html ast small tiny micro terse

zenhand's Introduction

Zenhand Travis Build Status

Zenhand parses a shorthand CSS selector like string representing an HTML element, and returns an object describing that element.

Example

var {zenhand} = require('zenhand')
// import {zenhand} from 'zenhand'  // If you're using es modules.

var obj = zenhand('div#ex.exmpl.info[style=background:red;color:black][data-name=temp]')
console.log(obj)

output:

{ tag: 'div',
  attrs: 
   { class: [ 'exmpl', 'info' ],
     style: { background: 'red', color: 'black' },
     id: 'ex',
     'data-name': 'temp' } }

Install

npm install --save zenhand

or

yarn add zenhand

API

zenhand(str[, opts])

  • str The CSS selector like string to convert into an object representing the HTML element. The str is made up of the following parts:
    • The very first part of the string can be an HTML tag, if left out it defaults to div.
    • # defines an id, should only have one, duplicates will overwrite one another, can be placed anywhere in the string after the tag.
    • . defines a class, multiples allowed, duplicates will be added to the class property, can be placed anywhere in the string after the tag.
    • [attr=val] place an attribute definition inside square brackets, multiples allowed, can be placed anywhere in the string after the tag.
  • opts An options object.
    • changeStyleCase If true automatic conversion between camelCase and kebab-case for property names, default is true.
  • return An object representing the HTML element.

The module also exports two helper functions; toStyleStr and fromStyleStr.

toStyleStr(obj[, fromCase, toCase])

Convert an object representation of CSS styles into a string, optionally converting property case.

var {toStyleStr, zenhand} = require('zenhand')
// Input.
var obj = {
  position: 'absolute',
  'backgroundColor': '#ff0000',
}

console.log(toStyleStr(obj, 'camel', 'kebab'))

// Output.
'position:absolute; background-color:#ff0000;'

Supports camel for camelCase, kebab for kebab-case, and snake for snake_case.

fromStyleStr(str[, fromCase, toCase])

Convert a str representation of CSS styles into an object, optionally converting property case.

var {fromStyleStr, zenhand} = require('zenhand')
// Input.
var str = 'position:absolute; background-color:#ff0000;'

console.log(fromStyleStr(str, 'kebab', 'camel'))

// Output.
{
  position: 'absolute',
  'backgroundColor': '#ff0000',
}

Supports camel for camelCase, kebab for kebab-case, and snake for snake_case.

zenhand's People

Contributors

arccoza avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

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.