add writeObjectToYMap and writeArrayToYArray helper utilities

This commit is contained in:
Kevin Jahns 2017-10-07 00:39:26 +02:00
parent c8ca80d15f
commit 23d019c244
2 changed files with 31 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import Y from '../src/y.js'
import yArray from '../../y-array/src/y-array.js'
import yIndexedDB from '../../y-indexeddb/src/y-indexeddb.js'
import yMap from '../../y-map/src/y-map.js'
import yText from '../../y-text/src/Text.js'
import yText from '../../y-text/src/y-text.js'
import yXml from '../../y-xml/src/y-xml.js'
import yWebsocketsClient from '../../y-websockets-client/src/y-websockets-client.js'

View File

@ -902,4 +902,34 @@ export default function Utils (Y) {
}
return args
}
Y.utils.writeObjectToYMap = function writeObjectToYMap (object, type) {
for (var key in object) {
var val = object[key]
if (Array.isArray(val)) {
type.set(key, Y.Array)
Y.utils.writeArrayToYArray(val, type.get(key))
} else if (typeof val === 'object') {
type.set(key, Y.Map)
Y.utils.writeObjectToYMap(val, type.get(key))
} else {
type.set(key, val)
}
}
}
Y.utils.writeArrayToYArray = function writeArrayToYArray (array, type) {
for (var i = array.length - 1; i >= 0; i--) {
var val = array[i]
if (Array.isArray(val)) {
type.insert(0, [Y.Array])
Y.utils.writeArrayToYArray(val, type.get(0))
} else if (typeof val === 'object') {
type.insert(0, [Y.Map])
Y.utils.writeObjectToYMap(val, type.get(0))
} else {
type.insert(0, [val])
}
}
}
}