Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 17, 2018
1 parent afa3356 commit 4a38f2c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
hast-util-has-property.js
hast-util-has-property.min.js
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
'use strict';
'use strict'

var own = {}.hasOwnProperty;
var own = {}.hasOwnProperty

module.exports = hasProperty;
module.exports = hasProperty

/* Check if `node` has a set `name` property. */
function hasProperty(node, name) {
var props;
var value;
var props
var value

if (!node || !name || typeof node !== 'object' || node.type !== 'element') {
return false;
return false
}

props = node.properties;
value = props && own.call(props, name) && props[name];
props = node.properties
value = props && own.call(props, name) && props[name]

return value !== null && value !== undefined && value !== false;
return value !== null && value !== undefined && value !== false
}
20 changes: 14 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,31 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^12.0.0",
"prettier": "^1.13.5",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.4.0",
"xo": "^0.21.0"
},
"scripts": {
"build-md": "remark . --quiet --frail --output",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js --bare -s hastUtilHasProperty > hast-util-has-property.js",
"build-mangle": "esmangle hast-util-has-property.js > hast-util-has-property.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"test-api": "node test.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"ignores": [
"hast-util-has-property.js"
Expand Down
40 changes: 23 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ npm install hast-util-has-property
## Usage

```javascript
var has = require('hast-util-has-property');

has({type: 'text', value: 'alpha'}, 'bravo'); //=> false

has({
type: 'element',
tagName: 'div',
properties: {id: 'bravo'},
children: []
}, 'className'); //=> false

has({
type: 'element',
tagName: 'div',
properties: {id: 'charlie'},
children: []
}, 'id'); // => true
var has = require('hast-util-has-property')

has({type: 'text', value: 'alpha'}, 'bravo') // => false

has(
{
type: 'element',
tagName: 'div',
properties: {id: 'bravo'},
children: []
},
'className'
) // => false

has(
{
type: 'element',
tagName: 'div',
properties: {id: 'charlie'},
children: []
},
'id'
) // => true
```

## API
Expand Down
48 changes: 27 additions & 21 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
'use strict';
'use strict'

var test = require('tape');
var hasProperty = require('.');
var test = require('tape')
var hasProperty = require('.')

test('hasProperty', function (t) {
test('hasProperty', function(t) {
t.equal(
hasProperty(null, 'alpha'),
false,
'should return `false` without node'
);
)

t.equal(
hasProperty({type: 'text', value: 'alpha'}, 'bravo'),
false,
'should return `false` without `element`'
);
)

t.equal(
hasProperty({type: 'element'}, 'charlie'),
false,
'should return `false` without properties'
);
)

t.equal(
hasProperty({type: 'element', properties: {}}, 'toString'),
false,
'should return `false` for prototypal properties'
);
)

t.equal(
hasProperty({
type: 'element',
properties: {id: 'delta'}
}, 'echo'),
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'echo'
),
false,
'should return `false` if the property does not exist'
);
)

t.equal(
hasProperty({
Expand All @@ -44,16 +47,19 @@ test('hasProperty', function (t) {
}),
false,
'should return `false` if without `name`'
);
)

t.equal(
hasProperty({
type: 'element',
properties: {id: 'delta'}
}, 'id'),
hasProperty(
{
type: 'element',
properties: {id: 'delta'}
},
'id'
),
true,
'should return `true` if the property does exist'
);
)

t.end();
});
t.end()
})

0 comments on commit 4a38f2c

Please sign in to comment.