Skip to content

Commit

Permalink
allow custom class name generation function
Browse files Browse the repository at this point in the history
  • Loading branch information
kof committed Jun 14, 2016
1 parent c7c092b commit d9ce34a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Jss.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import hash from 'murmurhash-js/murmurhash3_gc'
import StyleSheet from './StyleSheet'
import PluginsRegistry from './PluginsRegistry'
import SheetsRegistry from './SheetsRegistry'
import createRule from './createRule'
import findRenderer from './findRenderer'
import {generateClassName} from './utils'

/**
* Main Jss class.
Expand All @@ -15,7 +15,7 @@ export default class Jss {
this.sheets = new SheetsRegistry()
this.plugins = new PluginsRegistry()
this.version = process.env.VERSION
this.hash = options.hash || hash
this.generateClassName = options.generateClassName || generateClassName
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/rules/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default class Rule {
// It is also the fastetst way.
// http://jsperf.com/lodash-deepclone-vs-jquery-extend-deep/6
const styleStr = stringify(style)
const hash = options.jss.hash(styleStr)
this.style = parse(styleStr)
this.type = 'regular'
this.options = options
Expand All @@ -25,7 +24,7 @@ export default class Rule {
if (options.named) {
this.name = selector
if (!this.className) {
this.className = this.name ? `${this.name}-${hash}` : hash
this.className = options.jss.generateClassName(styleStr, this)
}
this.selectorText = `.${this.className}`
}
Expand Down
17 changes: 15 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import createHash from 'murmurhash-js/murmurhash3_gc'

/**
* Generates a class name using murmurhash.
*
* @param {String} str
* @param {Rule} rule
* @return {String}
*/
export function generateClassName(str, rule) {
const hash = createHash(str)
return rule.name ? `${rule.name}-${hash}` : hash
}

/**
* Determine whether an object is empty or not.
* More performant than a `Object.keys(obj).length > 0`
*
* @type {Object} obj
* @param {Object} obj
* @return {Boolean}
*/
export function isEmptyObject(obj) {
Expand Down Expand Up @@ -83,4 +97,3 @@ export const findClassNames = (() => {
.replace(dotsRegExp, '')
}
})()

2 changes: 1 addition & 1 deletion tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ export function reset() {
}

// Mock the hash function.
jss.hash = () => 'id'
jss.generateClassName = (str, rule) => rule.name ? `${rule.name}-id` : 'id'

0 comments on commit d9ce34a

Please sign in to comment.