summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authormustiikhalil <mustii@mmk.one>2020-07-27 19:57:50 +0300
committerGitHub <noreply@github.com>2020-07-27 09:57:50 -0700
commitf1025b284735d184f4a22dfe999ad65bfe558096 (patch)
tree05c0555471fe83764ffffba370564c115fb3fb19 /js
parent5d052f4e55d28117c208898cbfd96dc7ea37678d (diff)
downloadflatbuffers-f1025b284735d184f4a22dfe999ad65bfe558096.tar.gz
flatbuffers-f1025b284735d184f4a22dfe999ad65bfe558096.tar.bz2
flatbuffers-f1025b284735d184f4a22dfe999ad65bfe558096.zip
[Feature] Checks for Nullable strings (#6050)
* Allows null strings in createString method c# * Adds nullable strings to JS and swift * Changes js checks * Fixes typo
Diffstat (limited to 'js')
-rw-r--r--js/flatbuffers.js24
1 files changed, 24 insertions, 0 deletions
diff --git a/js/flatbuffers.js b/js/flatbuffers.js
index 596da800..b1687b75 100644
--- a/js/flatbuffers.js
+++ b/js/flatbuffers.js
@@ -252,6 +252,7 @@ flatbuffers.Builder.prototype.clear = function() {
this.vtables = [];
this.vector_num_elems = 0;
this.force_defaults = false;
+ this.string_maps = null;
};
/**
@@ -774,6 +775,28 @@ flatbuffers.Builder.prototype.endVector = function() {
/// @endcond
/**
+ * Encode the string `s` in the buffer using UTF-8. If the string passed has
+ * already been seen, we return the offset of the already written string
+ *
+ * @param {string|Uint8Array} s The string to encode
+ * @return {flatbuffers.Offset} The offset in the buffer where the encoded string starts
+ */
+flatbuffers.Builder.prototype.createSharedString = function(s) {
+ if (!s) { return 0 }
+
+ if (!this.string_maps) {
+ this.string_maps = new Map();
+ }
+
+ if (this.string_maps.has(s)) {
+ return this.string_maps.get(s)
+ }
+ let offset = this.createString(s)
+ this.string_maps.set(s, offset)
+ return offset
+}
+
+/**
* Encode the string `s` in the buffer using UTF-8. If a Uint8Array is passed
* instead of a string, it is assumed to contain valid UTF-8 encoded data.
*
@@ -781,6 +804,7 @@ flatbuffers.Builder.prototype.endVector = function() {
* @return {flatbuffers.Offset} The offset in the buffer where the encoded string starts
*/
flatbuffers.Builder.prototype.createString = function(s) {
+ if (!s) { return 0 }
if (s instanceof Uint8Array) {
var utf8 = s;
} else {