UNPKG

immutable-js

Version:
43 lines (37 loc) 1.29 kB
/** * Copyright (c) 2015, Jan Biasi. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ import { KeyedSequence, IndexedSequence } from '../Sequence'; import { isNative } from '../Native'; export function fromJS(native, converter) { return converter ? fromJSWith(converter, native, '', {'': native}) : fromJSDefault(native); } function fromJSWith(converter, native, key, parent) { if(Array.isArray(native)) { return converter.call(parent, key, IndexedSequence(native).map((v, k) => { fromJSWith(converter, v, k, native); })); } else if(maybePlainObject(native)) { return converter.call(parent, key, KeyedSequence(native).map((v, k) => { fromJSWith(converter, v, k, native); })); } } function fromJSDefault(json) { if(Array.isArray(json)) { return IndexedSequence(json); } if(maybePlainObject(json)) { return KeyedSequence(json); } } function maybePlainObject(value) { return value && (value.constructor === Object || value.constructor === undefined); }