Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
[added] the Markup.flatten method
Browse files Browse the repository at this point in the history
  • Loading branch information
thealjey committed Feb 4, 2016
1 parent 02c9096 commit 4a044cf
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 41 deletions.
198 changes: 188 additions & 10 deletions docs/Markup.html
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-15">line 15</a>
<a href="Markup.js.html#sunlight-1-line-17">line 17</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -265,6 +265,188 @@ <h3 class="subsection-title">Methods</h3>

<dl>

<hr>
<dt>
<h4 class="name" id=".flatten"><span class="type-signature">&lt;static> </span>flatten(args)</h4>


</dt>
<dd>


<div class="description">
<p>Recursively flattens <code>args</code> and combines string values.</p>
<p>Can be used as a simple optimization step on the JSX children-to-be to simplify the resulting DOM structure by
joining adjacent text nodes together.</p>
</div>








<h5>Parameters:</h5>


<table class="params table table-striped">
<thead>
<tr>

<th>Name</th>


<th>Type</th>


<th>Argument</th>




<th class="last">Description</th>
</tr>
</thead>

<tbody>


<tr>

<td class="name"><code>args</code></td>


<td class="type">


<span class="param-type">*</span>




</td>


<td class="attributes">





&lt;repeatable><br>

</td>




<td class="description last"><p>the input array</p></td>
</tr>


</tbody>
</table>




<dl class="details">





























<dt class="tag-source method-doc-label method-doc-details-label">Source:</dt>
<dd class="tag-source">
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-148">line 148</a>
</li>
</ul>
</dd>







</dl>













<h5>Returns:</h5>


<div class="param-desc">
<p>the flattened result</p>
</div>



<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">Array.&lt;*></span>



</dd>
</dl>





<h5>Example</h5>

<pre class="sunlight-highlight-javascript">Markup.flatten(&#x27;lorem &#x27;, [&#x27;ipsum &#x27;, [&#x27;dolor &#x27;, [&#x27;sit &#x27;, [&#x27;amet&#x27;]]]]); // [&quot;lorem ipsum dolor sit amet&quot;]</pre>



</dd>



<hr>
<dt>
<h4 class="name" id="htmlToJSX"><span class="type-signature"></span>htmlToJSX(<span class="optional">html</span>)</h4>
Expand Down Expand Up @@ -392,7 +574,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-160">line 160</a>
<a href="Markup.js.html#sunlight-1-line-188">line 188</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -445,9 +627,7 @@ <h5>Returns:</h5>

<h5>Example</h5>

<pre class="sunlight-highlight-javascript">const children = mark.htmlToJSX(&#x27;Hello &lt;span&gt;world!&lt;/span&gt;&#x27;);

return &lt;div&gt;{children}&lt;/div&gt;;</pre>
<pre class="sunlight-highlight-javascript">&lt;div&gt;{mark.htmlToJSX(&#x27;Hello &lt;span&gt;world!&lt;/span&gt;&#x27;)}&lt;/div&gt;</pre>



Expand Down Expand Up @@ -582,7 +762,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-177">line 177</a>
<a href="Markup.js.html#sunlight-1-line-203">line 203</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -770,7 +950,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="Markup.js.html">Markup.js</a>,
<a href="Markup.js.html#sunlight-1-line-192">line 192</a>
<a href="Markup.js.html#sunlight-1-line-218">line 218</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -823,9 +1003,7 @@ <h5>Returns:</h5>

<h5>Example</h5>

<pre class="sunlight-highlight-javascript">const header = mark.markdownToJSX(&#x27;# Hello world!&#x27;);

return &lt;div&gt;{header}&lt;/div&gt;;</pre>
<pre class="sunlight-highlight-javascript">&lt;div&gt;{mark.markdownToJSX(&#x27;# Hello world!&#x27;)}&lt;/div&gt;</pre>



Expand Down
36 changes: 30 additions & 6 deletions docs/Markup.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ <h1 class="page-title">Source: Markup.js</h1>
import transform from 'lodash/transform';
import has from 'lodash/has';
import reduce from 'lodash/reduce';
import flattenDeep from 'lodash/flattenDeep';
import isString from 'lodash/isString';

/* eslint-disable no-arrow-condition */

Expand Down Expand Up @@ -220,6 +222,32 @@ <h1 class="page-title">Source: Markup.js</h1>
return map(reject(children, ['type', 'comment']), (c, i) => 'text' === c.type ? c.data : Markup.childToJSX(c, i));
}

/**
* Recursively flattens `args` and combines string values.
*
* Can be used as a simple optimization step on the JSX children-to-be to simplify the resulting DOM structure by
* joining adjacent text nodes together.
*
* @memberof Markup
* @static
* @method flatten
* @param {...*} args - the input array
* @return {Array&lt;*>} the flattened result
* @example
* Markup.flatten('lorem ', ['ipsum ', ['dolor ', ['sit ', ['amet']]]]); // ["lorem ipsum dolor sit amet"]
*/
static flatten(...args: Array&lt;any>): Array&lt;any> {
return transform(flattenDeep(args), (accumulator, value) => {
const lastIndex = accumulator.length - 1;

if (isString(value) &amp;&amp; isString(accumulator[lastIndex])) {
accumulator[lastIndex] += value;
} else {
accumulator.push(value);
}
}, []);
}

/**
* Runs the html string through an array of Transformer functions
*
Expand All @@ -243,9 +271,7 @@ <h1 class="page-title">Source: Markup.js</h1>
* @param {string} [html=""] - an arbitrary HTML string
* @return {Array&lt;ReactElement>} an array of React Elements
* @example
* const children = mark.htmlToJSX('Hello &lt;span>world!&lt;/span>');
*
* return &lt;div>{children}&lt;/div>;
* &lt;div>{mark.htmlToJSX('Hello &lt;span>world!&lt;/span>')}&lt;/div>
*/
htmlToJSX(html: string = ''): Array&lt;any> {
return html ? Markup.childrenToJSX(load(this.transform(html)).root().toArray()[0].children) : [];
Expand Down Expand Up @@ -275,9 +301,7 @@ <h1 class="page-title">Source: Markup.js</h1>
* @param {string} [markdown=""] - an arbitrary Markdown string
* @return {Array&lt;ReactElement>} an array of React Elements
* @example
* const header = mark.markdownToJSX('# Hello world!');
*
* return &lt;div>{header}&lt;/div>;
* &lt;div>{mark.markdownToJSX('# Hello world!')}&lt;/div>
*/
markdownToJSX(markdown: string = ''): Array&lt;any> {
return markdown ? this.htmlToJSX(marked(markdown)) : [];
Expand Down
2 changes: 1 addition & 1 deletion docs/quicksearch.html

Large diffs are not rendered by default.

34 changes: 22 additions & 12 deletions interfaces/lodash.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* @flow */

type LoDashTransformCallback = (accumulator: any, value: any, key: number|string, collection: Array<any>|Object) => any;
type LoDashCallback = (value: any, key: number|string, collection: Array<any>|Object) => any;
type LoDashIteratee = LoDashCallback|Object|Array<any>|string;
type TransformCallback = (accumulator: any, value: any, key: number|string, collection: Array<any>|Object) => any;
type Callback = (value: any, key: number|string, collection: Array<any>|Object) => any;
type Iteratee = Callback|Object|Array<any>|string;
type Iterable = Array<any>|Object;
type Path = string|Array<string>;

declare module 'lodash/noop' {
declare function exports(...args: Array<any>): void;
Expand Down Expand Up @@ -32,38 +34,46 @@ declare module 'lodash/uniq' {
declare function exports(collection: Array<any>): Array<any>;
}

declare module 'lodash/flattenDeep' {
declare function exports(collection: Array<any>): Array<any>;
}

declare module 'lodash/without' {
declare function exports(collection: Array<any>, ...values: Array<any>): Array<any>;
}

declare module 'lodash/has' {
declare function exports(object: Object, path: string|Array<string>): boolean;
declare function exports(object: Object, path: Path): boolean;
}

declare module 'lodash/get' {
declare function exports(object: Object, path: string|Array<string>, defaultValue: ?any): any;
declare function exports(object: Object, path: Path, defaultValue: ?any): any;
}

declare module 'lodash/forEach' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashCallback): Array<any>|Object;
declare function exports(collection: Iterable, iteratee: ?Callback): Iterable;
}

declare module 'lodash/filter' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashIteratee): Array<any>;
declare function exports(collection: Iterable, iteratee: ?Iteratee): Array<any>;
}

declare module 'lodash/reject' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashIteratee): Array<any>;
declare function exports(collection: Iterable, iteratee: ?Iteratee): Array<any>;
}

declare module 'lodash/map' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashIteratee): Array<any>;
declare function exports(collection: Iterable, iteratee: ?Iteratee): Array<any>;
}

declare module 'lodash/find' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashIteratee): any;
declare function exports(collection: Iterable, iteratee: ?Iteratee): any;
}

declare module 'lodash/transform' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashTransformCallback, accumulator: ?any): any;
declare function exports(collection: Iterable, iteratee: ?TransformCallback, accumulator: ?any): any;
}

declare module 'lodash/reduce' {
declare function exports(collection: Array<any>|Object, iteratee: ?LoDashTransformCallback, accumulator: ?any): any;
declare function exports(collection: Iterable, iteratee: ?TransformCallback, accumulator: ?any): any;
}
1 change: 1 addition & 0 deletions interfaces/webcompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare module 'webcompiler' {

declare class Markup {
constructor(...transformers: Array<Transformer>): void;
static flatten(...args: Array<any>): Array<any>;
htmlToJSX(html: ?string): Array<any>;
markdownToHTML(markdown: ?string): string;
markdownToJSX(markdown: ?string): Array<any>;
Expand Down
Loading

0 comments on commit 4a044cf

Please sign in to comment.