Skip to content

Commit

Permalink
Change contains()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 31, 2018
1 parent 8280877 commit a0ca388
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 39 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,9 @@ Returns `true` or `false` or (on the CLI) use the exit code `0` or `1`.
```js
unixPermissions.contains('--------x', 'o+x') // `true`
unixPermissions.contains('--------x', 'o-x') // `false`
unixPermissions.contains('--------x', 'o-w') // `true`
unixPermissions.contains('--------x', 'o-w') // `false`
unixPermissions.contains('o+x', 'o+x') // `true`
unixPermissions.contains('o+x', 'o+x,o+x') // `true`
unixPermissions.contains('o+x', 'o-w') // `true`
unixPermissions.contains('o+x', 'o=w') // `false`
unixPermissions.contains('o+x,o-w', 'o+x,o-w') // `true`
unixPermissions.contains('o+x,o-w', 'o-w') // `true`
Expand All @@ -311,7 +310,6 @@ unixPermissions.equal('--------x', 'o-x') // `false`
unixPermissions.equal('--------x', 'o-w') // `false`
unixPermissions.equal('o+x', 'o+x') // `true`
unixPermissions.equal('o+x', 'o+x,o+x') // `true`
unixPermissions.equal('o+x', 'o-w') // `false`
unixPermissions.equal('o+x', 'o=w') // `false`
unixPermissions.equal('o+x,o-w', 'o+x,o-w') // `true`
unixPermissions.equal('o+x,o-w', 'o-w') // `false`
Expand Down
20 changes: 5 additions & 15 deletions src/functions/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,23 @@

const { binaryTest } = require('../helpers')

const { fullMap } = require('./full')

// Test whether all permissions in `nodesMapB` are included in `nodesMapA`
// Missing permissions in `nodesMapB` are not checked.
// `+` permissions in `nodesMapB` must be `+` in `nodesMapA`
// `-` permissions in `nodesMapB` must be either `-` or missing in `nodesMapA`
// `-` permissions in `nodesMapB` must be `-` in `nodesMapA`
const containsTest = function(nodesMapA, nodesMapB) {
// The first argument is set to its full shape.
// The reason is:
// - using `add: false` in nodesMapB must mean:
// - `add === undefined` if `nodesMapA` is a `stat` or `number`
// - either `add === false` or `add === undefined` if `nodesMapA` is a
// `symbolic` or `object`
// - we force the `add === false|undefined` meaning by using `fullMap()`
const nodesMapC = fullMap(nodesMapA)

return Object.entries(nodesMapB).every(([nodeKey, node]) =>
containsNode({ nodesMap: nodesMapC, nodeKey, node }),
containsNode(node, nodesMapA[nodeKey]),
)
}

const containsNode = function({ nodesMap, nodeKey, node }) {
return node.add === nodesMap[nodeKey].add
const containsNode = function(nodeA = {}, nodeB = {}) {
return nodeA.add === nodeB.add
}

const contains = binaryTest.bind(null, containsTest)

module.exports = {
contains,
containsTest,
}
16 changes: 3 additions & 13 deletions src/functions/equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,19 @@

const { binaryTest } = require('../helpers')

const { containsTest } = require('./contains')

// Test whether two permissions are exactly the same, including omitted nodes.
// As opposed to using `===`, this works across permissions. It also works
// with non-canonical variations, e.g. `equal('a+x,a+x', 'a+x')` is `true`
const equalTest = function(nodesMapA, nodesMapB) {
return sameLength(nodesMapA, nodesMapB) && sameNodes(nodesMapA, nodesMapB)
return sameLength(nodesMapA, nodesMapB) && containsTest(nodesMapA, nodesMapB)
}

const sameLength = function(nodesMapA, nodesMapB) {
return Object.keys(nodesMapA).length === Object.keys(nodesMapB).length
}

const sameNodes = function(nodesMapA, nodesMapB) {
return Object.entries(nodesMapA).every(([nodeKey, node]) =>
isSameNode(node, nodesMapB[nodeKey]),
)
}

const isSameNode = function(nodeA = {}, nodeB = {}) {
return ATTRIBUTES.every(attribute => nodeA[attribute] === nodeB[attribute])
}

const ATTRIBUTES = ['category', 'permission', 'add']

const equal = binaryTest.bind(null, equalTest)

module.exports = {
Expand Down
1 change: 0 additions & 1 deletion src/functions/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ const full = unaryMap.bind(null, fullMap)

module.exports = {
full,
fullMap,
}
2 changes: 2 additions & 0 deletions test/helpers/data/contains.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const CONTAINS_DATA = [

// Combining
['o+x,o-r', 'o+x,o-r'],
['o+x,o-r', 'o+x'],
['o+x,o-r', 'o-r'],
['o+x', 'o+x,o-r'],
['o-r', 'o+x,o-r'],

Expand Down
32 changes: 25 additions & 7 deletions test/snapshots/contains.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Generated by [AVA](https://ava.li).
{
error: false,
output: true,
output: false,
}

## should test whether o+t contains o+t
Expand All @@ -78,6 +78,15 @@ Generated by [AVA](https://ava.li).

## should test whether o+t contains o-t

> Snapshot 1
{
error: false,
output: false,
}

## should test whether o+x contains

> Snapshot 1
{
Expand Down Expand Up @@ -118,7 +127,7 @@ Generated by [AVA](https://ava.li).
{
error: false,
output: true,
output: false,
}

## should test whether o+x contains o-x
Expand Down Expand Up @@ -211,20 +220,29 @@ Generated by [AVA](https://ava.li).
output: false,
}

## should test whether o+x contains
## should test whether undefined contains

> Snapshot 1
{
error: true,
output: 'Permissions syntax is invalid: undefined',
}

## should test whether o+x,o-r contains o+x

> Snapshot 1
{
error: false,
output: false,
output: true,
}

## should test whether undefined contains
## should test whether o+x,o-r contains o-r

> Snapshot 1
{
error: true,
output: 'Permissions syntax is invalid: undefined',
error: false,
output: true,
}
Binary file modified test/snapshots/contains.js.snap
Binary file not shown.

0 comments on commit a0ca388

Please sign in to comment.