obj-str 一个用于将对象值序列化为字符串的小型(117b)库

网友投稿 873 2022-10-26

obj-str 一个用于将对象值序列化为字符串的小型(117b)库

obj-str 一个用于将对象值序列化为字符串的小型(117b)库

A tiny (117b) library for serializing Object values to Strings.

This module's intended use is for converting an Object with CSS class names (as keys) to a space-delimited className string. Other modules have similar goals (like classnames), but obj-str only does one thing. This is why it's only 117 bytes gzipped!

PS: I made this because Preact 8.0 removed this built-in behavior and I wanted a quick, drop-in replacement.

Install

$ npm install --save obj-str

Usage

import objstr from 'obj-str';objstr({ foo:true, bar:false, baz:isTrue() });//=> 'foo baz'

React

With React (or any of the React-like libraries!), you can take advantage of any props or state values in order to express conditional classes as an object.

import React from 'react';import objstr from 'obj-str';const TodoItem = ({ text, isDone, disabled }) => (

  • );

    Preact

    For simple use, the React example will work for Preact too. However, you may also define a custom vNode "polyfill" to automatically handle Objects when used inside className.

    Note: For users of Preact 7.1 and below, you do not need this module! Your version includes this behavior out of the box!

    import objstr from 'obj-str';import { options } from 'preact';const old = options.vnode;options.vnode = vnode => { const props = vnode.attributes; if (props != null) { const k = 'class' in props ? 'class' : 'className'; if (props[k] && typeof props[k]=='object') { props[k] = objstr(props[k]); } } old && old(vnode);}

    API

    objstr(input)

    input

    Type: Object

    A hashmap of keys & their truthy/falsey values. Booleans are preferred when speed is critically important.

    License

    MIT © Luke Edwards

    版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

    上一篇:基于 Qt 的图表库
    下一篇:Spring cloud如何实现FeignClient指定Zone调用
    相关文章

     发表评论

    暂时没有评论,来抢沙发吧~