Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add booleans #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[![NPM version](https://badge.fury.io/js/slate-hyperprint.svg)](http://badge.fury.io/js/slate-hyperprint)

Slate's data model has been built with serialization in mind. Specifically, its text nodes are defined in a way that makes them easier to read at a glance, but also easy to serialize to common formats like HTML and Markdown.
And, because Slate uses plain JSON for its data, you can write serialization logic very easily.

A library to convert Slate models to their slate-hyperscript representation.

You can use `slate-hyperprint` as a library to:
Expand Down
57 changes: 57 additions & 0 deletions src/bin/booleans.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @flow
import isPlainObject from 'is-plain-object';
import printComplexDataStructure from './printComplexDataStructure';

/*
Based on
https://github.com/algolia/react-element-to-jsx-string/blob/master/src/formatter/formatPropValue.js
*/

const escape = (s: string): string => s.replace(/"/g, '"');

/*
* Print a tag attribute, for example as 'key={value}' or 'key="value"' or 'key'
*/
function printAttributeValue(value: any): string {
if (typeof value === 'number') {
return `{${String(value)}}`;
}

if (typeof value === 'string') {
return `"${escape(value)}"`;
}

// > "Symbols (new in ECMAScript 2015, not yet supported in Flow)"
// @see: https://flow.org/en/docs/types/primitives/
// $FlowFixMe: Flow does not support Symbol
if (typeof value === 'symbol') {
throw new Error('not implemented');
}

if (typeof value === 'function') {
throw new Error('not implemented');
}

if (value instanceof Date) {
return `{new Date("${value.toISOString()}")}`;
}

if (isPlainObject(value) || Array.isArray(value)) {
return `{${printComplexDataStructure(value)}}`;
}

return `{${String(value)}}`;
}

/*
* Print a tag attribute to 'key={value}' or 'key' for `true`
*/
function printAttribute(key: string, value: any) {
if (value === true) {
return key;
}

return `${key}=${printAttributeValue(value)}`;
}

export default printAttribute;