Skip to content
Permalink
Browse files

Added new special key: '_eachId_'.

  • Loading branch information...
marcomontalbano committed Aug 27, 2018
1 parent 3ca572e commit 6b00bb10dfbf824ebd61764b3886f62ad6cb21f0
Showing with 641 additions and 533 deletions.
  1. +43 −6 README.md
  2. +1 −1 docs/app.min.js
  3. +6 −3 examples/demo.js
  4. +10 −3 lib/index.js
  5. +520 −507 package-lock.json
  6. +3 −3 package.json
  7. +3 −3 test/html/getbootstrap.html
  8. +5 −5 test/html/readme.md.html
  9. +24 −2 test/readme.md.js
  10. +26 −0 test/test.js
@@ -191,7 +191,7 @@ A `function` accepts only one argument that is an `object` containing:

When selector is an `object`, you can use _special keys_:

- `_each_`: creates a list of items. HTML Miner will iterate for the value e will parse siblings keys.
- `_each_`: creates a list of items. HTML Miner will iterate for the value and will parse siblings keys.

```js
{
@@ -203,6 +203,21 @@ When selector is an `object`, you can use _special keys_:
}
```

- `_eachId_`: useful when combined with `_each_`. Instead of creating an Array, it creates an Object where keys are the result of `_eachId_` function.

```js
{
articles: {
_each_: '.articles .article',
_eachId_: function(arg) {
return arg.$scope.data('id');
}
title: 'h2',
content: 'p',
}
}
```

- `_container_`: uses the parsed value as container. HTML Miner will parse siblings keys, searching them inside the _container_.

```js
@@ -225,15 +240,15 @@ Consider the following html snippet: we will try and fetch some information.
```html
<h1>Hello, <span>world</span>!</h1>
<div class="articles">
<div class="article">
<div class="article" data-id="a001">
<h2>Heading 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>
<div class="article">
<div class="article" data-id="a002">
<h2>Heading 2</h2>
<p>Donec maximus ipsum quis est tempor, sit amet laoreet libero bibendum.</p>
</div>
<div class="article">
<div class="article" data-id="a003">
<h2>Heading 3</h2>
<p>Suspendisse viverra convallis risus, vitae molestie est tincidunt eget.</p>
</div>
@@ -250,11 +265,19 @@ let json = htmlMiner(html, {
title: 'h1',
who: 'h1 span',
h2: 'h2',
articles: {
articlesArray: {
_each_: '.articles .article',
title: 'h2',
content: 'p',
},
articlesObject: {
_each_: '.articles .article',
_eachId_: function(arg) {
return arg.$scope.data('id');
},
title: 'h2',
content: 'p',
},
footer: {
_container_: 'footer',
copyright: (arg) => { return arg.$scope.text().trim(); },
@@ -270,7 +293,7 @@ console.log( json );
// title: 'Hello, world!',
// who: 'world',
// h2: ['Heading 1', 'Heading 2', 'Heading 3'],
// articles: [
// articlesArray: [
// {
// title: 'Heading 1',
// content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
@@ -284,6 +307,20 @@ console.log( json );
// content: 'Suspendisse viverra convallis risus, vitae molestie est tincidunt eget.',
// }
// ],
// articlesObject: {
// 'a001': {
// title: 'Heading 1',
// content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
// },
// 'a002': {
// title: 'Heading 2',
// content: 'Donec maximus ipsum quis est tempor, sit amet laoreet libero bibendum.',
// },
// 'a003': {
// title: 'Heading 3',
// content: 'Suspendisse viverra convallis risus, vitae molestie est tincidunt eget.',
// }
// },
// footer: {
// copyright: '© Company 2017',
// company: 'Company',

Large diffs are not rendered by default.

@@ -41,9 +41,12 @@ fs.readFile(path.join(__dirname, '../test/html/getbootstrap.html'), 'utf8', func
}
},
articles : {
_each_ : '.col-md-4',
title : 'h2',
text : 'p:first-of-type',
_each_ : '.col-md-4',
_eachId_ : function (arg) {
return arg.$scope.data('id');
},
title : 'h2',
text : 'p:first-of-type',
button : {
_container_: 'a.btn',
text: function(arg) { return arg.$scope.text(); },
@@ -34,7 +34,7 @@ var _fetchSpecialKeys = function($, selector, memo)

_each_ : function($, specialKeySelector, selector, memo) {

var elements = [], element;
var elements, element;
$(specialKeySelector).each(function(i, el) {
element = _htmlMiner(el, selector, {
$document : memo.$document,
@@ -43,8 +43,15 @@ var _fetchSpecialKeys = function($, selector, memo)
});

// remove empty objects.
if ( !_.isEmpty(element) ) {
elements.push( element );
if (!_.isEmpty(element)) {
if (element._eachId_) { // if _eachId_ is specified, use it as object ID
elements = elements || {};
elements[element._eachId_] = element;
delete element['_eachId_'];
} else {
elements = elements || [];
elements.push(element);
}
}
});
return elements;

0 comments on commit 6b00bb1

Please sign in to comment.
You can’t perform that action at this time.