Last active
November 6, 2017 00:18
-
-
Save Soreine/b4a9413e635a40cda9509f1d213d4e41 to your computer and use it in GitHub Desktop.
A function that convert an old Slate rule, to a single plugin object, containing the corresponding `validateNode` function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Converts an old rule definition to an individual plugin with a "validateNode" function | |
// | |
// rule: { | |
// match, | |
// validate, | |
// normalize | |
// } | |
// | |
// returns { | |
// validateNode | |
// } | |
function toValidateNode(rule) { | |
return { | |
validateNode: (node) => { | |
if (!rule.match(node)) { | |
return undefined; | |
} | |
const validationResult = rule.validate(node); | |
if (validationResult == null) { | |
return undefined; | |
} | |
return change => rule.normalize(change, node, validationResult); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice! 👏