diff --git a/src/js/models/_attributable.js b/src/js/models/_attributable.js index bea980c61..540505d69 100644 --- a/src/js/models/_attributable.js +++ b/src/js/models/_attributable.js @@ -1,3 +1,5 @@ +import { entries } from '../utils/object-utils'; + export const VALID_ATTRIBUTES = [ 'data-md-text-align' ]; @@ -20,6 +22,6 @@ export function attributable(ctx) { }; ctx.getAttribute = key => ctx.attributes[key]; ctx.eachAttribute = cb => { - Object.entries(ctx.attributes).forEach(([k,v]) => cb(k,v)); + entries(ctx.attributes).forEach(([k,v]) => cb(k,v)); }; } diff --git a/src/js/models/list-section.js b/src/js/models/list-section.js index 59efaf625..33d1f4313 100644 --- a/src/js/models/list-section.js +++ b/src/js/models/list-section.js @@ -1,10 +1,12 @@ -import LinkedList from '../utils/linked-list'; -import { forEach, contains } from '../utils/array-utils'; import { LIST_SECTION_TYPE } from './types'; import Section from './_section'; +import { attributable } from './_attributable'; + +import LinkedList from '../utils/linked-list'; +import { forEach, contains } from '../utils/array-utils'; import { normalizeTagName } from '../utils/dom-utils'; import assert from '../utils/assert'; -import { attributable } from './_attributable'; +import { entries } from '../utils/object-utils'; export const VALID_LIST_SECTION_TAGNAMES = [ 'ul', 'ol' @@ -20,7 +22,7 @@ export default class ListSection extends Section { this.isLeafSection = false; attributable(this); - Object.entries(attributes).forEach(([k,v]) => this.setAttribute(k, v)); + entries(attributes).forEach(([k,v]) => this.setAttribute(k, v)); this.items = new LinkedList({ adoptItem: i => { diff --git a/src/js/models/markup-section.js b/src/js/models/markup-section.js index 0766a19a7..5d46c4492 100644 --- a/src/js/models/markup-section.js +++ b/src/js/models/markup-section.js @@ -1,8 +1,10 @@ import Markerable from './_markerable'; +import { attributable } from './_attributable'; +import { MARKUP_SECTION_TYPE } from './types'; + import { normalizeTagName } from '../utils/dom-utils'; import { contains } from '../utils/array-utils'; -import { MARKUP_SECTION_TYPE } from './types'; -import { attributable } from './_attributable'; +import { entries } from '../utils/object-utils'; // valid values of `tagName` for a MarkupSection export const VALID_MARKUP_SECTION_TAGNAMES = [ @@ -38,7 +40,7 @@ const MarkupSection = class MarkupSection extends Markerable { super(MARKUP_SECTION_TYPE, tagName, markers); attributable(this); - Object.entries(attributes).forEach(([k,v]) => this.setAttribute(k, v)); + entries(attributes).forEach(([k,v]) => this.setAttribute(k, v)); this.isMarkupSection = true; } diff --git a/src/js/parsers/mobiledoc/0-3-2.js b/src/js/parsers/mobiledoc/0-3-2.js index 877df5692..7f9c65344 100644 --- a/src/js/parsers/mobiledoc/0-3-2.js +++ b/src/js/parsers/mobiledoc/0-3-2.js @@ -5,9 +5,11 @@ import { MOBILEDOC_CARD_SECTION_TYPE, MOBILEDOC_MARKUP_MARKER_TYPE, MOBILEDOC_ATOM_MARKER_TYPE -} from 'mobiledoc-kit/renderers/mobiledoc/0-3-2'; -import { kvArrayToObject, filter } from "../../utils/array-utils"; -import assert from 'mobiledoc-kit/utils/assert'; +} from '../../renderers/mobiledoc/0-3-2'; + +import { kvArrayToObject, filter } from '../../utils/array-utils'; +import assert from '../../utils/assert'; +import { entries } from '../../utils/object-utils'; /* * Parses from mobiledoc -> post @@ -113,7 +115,7 @@ export default class MobiledocParser { const section = this.builder.createMarkupSection(tagName); post.sections.append(section); if (attributesArray) { - Object.entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => { + entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => { section.setAttribute(key, value); }); } @@ -129,7 +131,7 @@ export default class MobiledocParser { const section = this.builder.createListSection(tagName); post.sections.append(section); if (attributesArray) { - Object.entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => { + entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => { section.setAttribute(key, value); }); } diff --git a/src/js/utils/object-utils.js b/src/js/utils/object-utils.js new file mode 100644 index 000000000..1f5b5ff14 --- /dev/null +++ b/src/js/utils/object-utils.js @@ -0,0 +1,11 @@ +export function entries(obj) { + const ownProps = Object.keys(obj); + let i = ownProps.length; + const resArray = new Array(i); + + while (i--) { + resArray[i] = [ownProps[i], obj[ownProps[i]]]; + } + + return resArray; +} diff --git a/tests/unit/utils/object-utils-test.js b/tests/unit/utils/object-utils-test.js new file mode 100644 index 000000000..ab11cf7ae --- /dev/null +++ b/tests/unit/utils/object-utils-test.js @@ -0,0 +1,13 @@ +import Helpers from '../../test-helpers'; +import { entries } from 'mobiledoc-kit/utils/object-utils'; + +const { module, test } = Helpers; + +module('Unit: Utils: Object Utils'); + +test('#entries works', assert => { + assert.deepEqual(entries({ hello: 'world', goodbye: 'moon' }), [ + ['hello', 'world'], + ['goodbye', 'moon'] + ]); +});