react-classlist-helper
Advanced tools
Comparing version 1.2.0 to 1.2.1
{ | ||
"name": "react-classlist-helper", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Helper to defining multiple classes on a react component.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/react-classlist-helper.min.js", |
@@ -10,7 +10,7 @@ # React classList Helper | ||
``` | ||
npm install react-classlist-helper --save-dev | ||
npm install react-classlist-helper --save | ||
``` | ||
Also available with Yarn: | ||
``` | ||
yarn add react-classlist-helper --dev | ||
yarn add react-classlist-helper | ||
``` | ||
@@ -20,3 +20,3 @@ | ||
### classList(class1: string | array | object, class2, class3, ...classN): string | ||
### classList(class1: (string || array || object), class2, ...classN): string | ||
Used to add multiple classes to an element: | ||
@@ -90,2 +90,2 @@ | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -6,4 +6,11 @@ import isBoolean from 'lodash.isboolean'; | ||
export function classList(...classes) { | ||
if (classes.length === 0) { | ||
/** | ||
* Spred css class names provided as input | ||
* | ||
* @param {Array} args The classe's names. | ||
* @return {string} final className | ||
*/ | ||
export function classList(...args) { | ||
if (args.length === 0) { | ||
return ''; | ||
@@ -14,5 +21,5 @@ } | ||
classes.forEach((className) => { | ||
args.forEach((className) => { | ||
if (Array.isArray(className)) { | ||
classBuffer = [...classBuffer, className.reduce((total, classItem) => `${total} ${classItem}`)]; | ||
classBuffer = [...classBuffer, className.reduce((total, curItem) => `${total} ${curItem}`)]; | ||
} else if (isString(className)) { | ||
@@ -36,2 +43,10 @@ classBuffer = [...classBuffer, className]; | ||
/** | ||
* Toggle a class based on a condition. | ||
* | ||
* @param {string} className The class name | ||
* @param {boolean} condition The condition | ||
* @return {string} if condition is true, returns the string, if false, | ||
* returns empty string | ||
*/ | ||
export function toggleClass(className, condition) { | ||
@@ -52,2 +67,4 @@ if (isUndefined(className) || !isString(className)) { | ||
export default classList; | ||
const defaultFunc = classList; | ||
export default defaultFunc; |
import { expect } from 'chai'; | ||
import classList from './index'; | ||
import { toggleClass } from './index'; | ||
import { classList, toggleClass } from './index'; | ||
@@ -79,8 +78,9 @@ | ||
expect(() => classList(1, 'string')).to.throw(); | ||
expect(() => classList(undefined)).to.throw(); | ||
}); | ||
it('should handle empty inputs gracefully', () => { | ||
const expectedOutput = ''; | ||
const output = classList(); | ||
const expectedOutput = ''; | ||
expect(output).to.equal(expectedOutput); | ||
@@ -87,0 +87,0 @@ expect(output).to.be.a('string'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15374
216
89