Skip to content

Commit

Permalink
Replace Object.entries with utility function 🥴
Browse files Browse the repository at this point in the history
A previous change introduced the use of Object.entries, which is not available in some browsers supported by Mobiledoc (most notably IE11). This commit replaces Object.entries with a utility function `entries` with the same functionality.
  • Loading branch information
ZeeJab committed Sep 20, 2019
1 parent 654943d commit ebed97a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/js/models/_attributable.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { entries } from '../utils/object-utils';

export const VALID_ATTRIBUTES = [
'data-md-text-align'
];
Expand All @@ -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));
};
}
10 changes: 6 additions & 4 deletions src/js/models/list-section.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 => {
Expand Down
8 changes: 5 additions & 3 deletions src/js/models/markup-section.js
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 7 additions & 5 deletions src/js/parsers/mobiledoc/0-3-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
});
}
Expand All @@ -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);
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/js/utils/object-utils.js
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions tests/unit/utils/object-utils-test.js
Original file line number Diff line number Diff line change
@@ -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']
]);
});

0 comments on commit ebed97a

Please sign in to comment.