Add m.censor, adjust m.route.Link to use it (#2538)

Also, restructure a few things for better code reuse.
This commit is contained in:
Isiah Meadows 2019-09-30 18:44:39 -04:00 committed by GitHub
parent 3fa1630f91
commit 34f4363357
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 488 additions and 55 deletions

47
util/censor.js Normal file
View file

@ -0,0 +1,47 @@
"use strict"
// Note: this is mildly perf-sensitive.
//
// It does *not* use `delete` - dynamic `delete`s usually cause objects to bail
// out into dictionary mode and just generally cause a bunch of optimization
// issues within engines.
//
// Ideally, I would've preferred to do this, if it weren't for the optimization
// issues:
//
// ```js
// const hasOwn = require("./hasOwn")
// const magic = [
// "key", "oninit", "oncreate", "onbeforeupdate", "onupdate",
// "onbeforeremove", "onremove",
// ]
// module.exports = (attrs, extras) => {
// const result = Object.assign(Object.create(null), attrs)
// for (const key of magic) delete result[key]
// if (extras != null) for (const key of extras) delete result[key]
// return result
// }
// ```
var hasOwn = require("./hasOwn")
var magic = /^(?:key|oninit|oncreate|onbeforeupdate|onupdate|onbeforeremove|onremove)$/
module.exports = function(attrs, extras) {
var result = {}
if (extras != null) {
for (var key in attrs) {
if (hasOwn.call(attrs, key) && !magic.test(key) && extras.indexOf(key) < 0) {
result[key] = attrs[key]
}
}
} else {
for (var key in attrs) {
if (hasOwn.call(attrs, key) && !magic.test(key)) {
result[key] = attrs[key]
}
}
}
return result
}