Skip to content

Commit

Permalink
Fix octal serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jan 6, 2019
1 parent d4c95c0 commit d8b0be4
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/types/octal/serialize.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
'use strict'

const { NODES_MAP, CAT_NODES_MAP } = require('../../nodes')
const number = require('../number')

const {
OCTAL_BASE,
SERIALIZE_LENGTH,
CAT_SERIALIZE_LENGTH,
SERIALIZE_PAD,
OPERATORS: { MINUS, EQUAL, NONE },
OPERATORS: { PLUS, MINUS, NONE },
} = require('./constants')

// Serialize from `nodes` to a `octal` permission
const serializePerm = function({ funcName, padLength }, nodes) {
const operator = serializeOperator({ nodes })
const serializePerm = function({ funcName, padLength, nodesMap }, nodes) {
const operator = serializeOperator({ nodes, nodesMap })
const string = serializeInteger({ operator, nodes, funcName, padLength })
const octal = `${operator}${string}`
return octal
}

// `octal` can be prefixed with `-` or `=`.
// `+` is the default operator, i.e. is never serialized.
const serializeOperator = function({ nodes }) {
// Including empty array
if (!nodes.some(isRemoved)) {
// `octal` can be prefixed with `-` or `+` if partial and only negative|positive
// `=` is the default operator, i.e. is never serialized.
const serializeOperator = function({ nodes, nodesMap }) {
if (!isPartial({ nodes, nodesMap })) {
return NONE
}

if (nodes.every(isAdded)) {
return PLUS
}

if (nodes.every(isRemoved)) {
return MINUS
}

return EQUAL
return NONE
}

const isPartial = function({ nodes, nodesMap }) {
return Object.keys(nodes).length !== Object.keys(nodesMap).length
}

const isAdded = function({ add }) {
return add === true
}

const isRemoved = function({ add }) {
return add !== true
return add === false
}

// Re-use `number` serialization logic, then stringify to an octal number
Expand All @@ -59,10 +71,12 @@ const serializeMinus = function({ operator, nodes }) {
const serialize = serializePerm.bind(null, {
funcName: 'serialize',
padLength: SERIALIZE_LENGTH,
nodesMap: NODES_MAP,
})
const serializeCategory = serializePerm.bind(null, {
funcName: 'serializeCategory',
padLength: CAT_SERIALIZE_LENGTH,
nodesMap: CAT_NODES_MAP,
})

module.exports = {
Expand Down

0 comments on commit d8b0be4

Please sign in to comment.