|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
var util = module.exports = require("./util/minimal"); |
|
|
|
var roots = require("./roots"); |
|
|
|
var Type, |
|
Enum; |
|
|
|
util.codegen = require("@protobufjs/codegen"); |
|
util.fetch = require("@protobufjs/fetch"); |
|
util.path = require("@protobufjs/path"); |
|
|
|
|
|
|
|
|
|
|
|
util.fs = util.inquire("fs"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.toArray = function toArray(object) { |
|
if (object) { |
|
var keys = Object.keys(object), |
|
array = new Array(keys.length), |
|
index = 0; |
|
while (index < keys.length) |
|
array[index] = object[keys[index++]]; |
|
return array; |
|
} |
|
return []; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.toObject = function toObject(array) { |
|
var object = {}, |
|
index = 0; |
|
while (index < array.length) { |
|
var key = array[index++], |
|
val = array[index++]; |
|
if (val !== undefined) |
|
object[key] = val; |
|
} |
|
return object; |
|
}; |
|
|
|
var safePropBackslashRe = /\\/g, |
|
safePropQuoteRe = /"/g; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.isReserved = function isReserved(name) { |
|
return /^(?:do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)$/.test(name); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.safeProp = function safeProp(prop) { |
|
if (!/^[$\w_]+$/.test(prop) || util.isReserved(prop)) |
|
return "[\"" + prop.replace(safePropBackslashRe, "\\\\").replace(safePropQuoteRe, "\\\"") + "\"]"; |
|
return "." + prop; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.ucFirst = function ucFirst(str) { |
|
return str.charAt(0).toUpperCase() + str.substring(1); |
|
}; |
|
|
|
var camelCaseRe = /_([a-z])/g; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.camelCase = function camelCase(str) { |
|
return str.substring(0, 1) |
|
+ str.substring(1) |
|
.replace(camelCaseRe, function($0, $1) { return $1.toUpperCase(); }); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
util.compareFieldsById = function compareFieldsById(a, b) { |
|
return a.id - b.id; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
util.decorateType = function decorateType(ctor, typeName) { |
|
|
|
|
|
if (ctor.$type) { |
|
if (typeName && ctor.$type.name !== typeName) { |
|
util.decorateRoot.remove(ctor.$type); |
|
ctor.$type.name = typeName; |
|
util.decorateRoot.add(ctor.$type); |
|
} |
|
return ctor.$type; |
|
} |
|
|
|
|
|
if (!Type) |
|
Type = require("./type"); |
|
|
|
var type = new Type(typeName || ctor.name); |
|
util.decorateRoot.add(type); |
|
type.ctor = ctor; |
|
Object.defineProperty(ctor, "$type", { value: type, enumerable: false }); |
|
Object.defineProperty(ctor.prototype, "$type", { value: type, enumerable: false }); |
|
return type; |
|
}; |
|
|
|
var decorateEnumIndex = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
util.decorateEnum = function decorateEnum(object) { |
|
|
|
|
|
if (object.$type) |
|
return object.$type; |
|
|
|
|
|
if (!Enum) |
|
Enum = require("./enum"); |
|
|
|
var enm = new Enum("Enum" + decorateEnumIndex++, object); |
|
util.decorateRoot.add(enm); |
|
Object.defineProperty(object, "$type", { value: enm, enumerable: false }); |
|
return enm; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
util.setProperty = function setProperty(dst, path, value) { |
|
function setProp(dst, path, value) { |
|
var part = path.shift(); |
|
if (part === "__proto__" || part === "prototype") { |
|
return dst; |
|
} |
|
if (path.length > 0) { |
|
dst[part] = setProp(dst[part] || {}, path, value); |
|
} else { |
|
var prevValue = dst[part]; |
|
if (prevValue) |
|
value = [].concat(prevValue).concat(value); |
|
dst[part] = value; |
|
} |
|
return dst; |
|
} |
|
|
|
if (typeof dst !== "object") |
|
throw TypeError("dst must be an object"); |
|
if (!path) |
|
throw TypeError("path must be specified"); |
|
|
|
path = path.split("."); |
|
return setProp(dst, path, value); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(util, "decorateRoot", { |
|
get: function() { |
|
return roots["decorated"] || (roots["decorated"] = new (require("./root"))()); |
|
} |
|
}); |
|
|