Skip to content

Commit ebed97a

Browse files
committed
Replace Object.entries with utility function 🥴
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.
1 parent 654943d commit ebed97a

File tree

6 files changed

+45
-13
lines changed

6 files changed

+45
-13
lines changed

src/js/models/_attributable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { entries } from '../utils/object-utils';
2+
13
export const VALID_ATTRIBUTES = [
24
'data-md-text-align'
35
];
@@ -20,6 +22,6 @@ export function attributable(ctx) {
2022
};
2123
ctx.getAttribute = key => ctx.attributes[key];
2224
ctx.eachAttribute = cb => {
23-
Object.entries(ctx.attributes).forEach(([k,v]) => cb(k,v));
25+
entries(ctx.attributes).forEach(([k,v]) => cb(k,v));
2426
};
2527
}

src/js/models/list-section.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import LinkedList from '../utils/linked-list';
2-
import { forEach, contains } from '../utils/array-utils';
31
import { LIST_SECTION_TYPE } from './types';
42
import Section from './_section';
3+
import { attributable } from './_attributable';
4+
5+
import LinkedList from '../utils/linked-list';
6+
import { forEach, contains } from '../utils/array-utils';
57
import { normalizeTagName } from '../utils/dom-utils';
68
import assert from '../utils/assert';
7-
import { attributable } from './_attributable';
9+
import { entries } from '../utils/object-utils';
810

911
export const VALID_LIST_SECTION_TAGNAMES = [
1012
'ul', 'ol'
@@ -20,7 +22,7 @@ export default class ListSection extends Section {
2022
this.isLeafSection = false;
2123

2224
attributable(this);
23-
Object.entries(attributes).forEach(([k,v]) => this.setAttribute(k, v));
25+
entries(attributes).forEach(([k,v]) => this.setAttribute(k, v));
2426

2527
this.items = new LinkedList({
2628
adoptItem: i => {

src/js/models/markup-section.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import Markerable from './_markerable';
2+
import { attributable } from './_attributable';
3+
import { MARKUP_SECTION_TYPE } from './types';
4+
25
import { normalizeTagName } from '../utils/dom-utils';
36
import { contains } from '../utils/array-utils';
4-
import { MARKUP_SECTION_TYPE } from './types';
5-
import { attributable } from './_attributable';
7+
import { entries } from '../utils/object-utils';
68

79
// valid values of `tagName` for a MarkupSection
810
export const VALID_MARKUP_SECTION_TAGNAMES = [
@@ -38,7 +40,7 @@ const MarkupSection = class MarkupSection extends Markerable {
3840
super(MARKUP_SECTION_TYPE, tagName, markers);
3941

4042
attributable(this);
41-
Object.entries(attributes).forEach(([k,v]) => this.setAttribute(k, v));
43+
entries(attributes).forEach(([k,v]) => this.setAttribute(k, v));
4244

4345
this.isMarkupSection = true;
4446
}

src/js/parsers/mobiledoc/0-3-2.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import {
55
MOBILEDOC_CARD_SECTION_TYPE,
66
MOBILEDOC_MARKUP_MARKER_TYPE,
77
MOBILEDOC_ATOM_MARKER_TYPE
8-
} from 'mobiledoc-kit/renderers/mobiledoc/0-3-2';
9-
import { kvArrayToObject, filter } from "../../utils/array-utils";
10-
import assert from 'mobiledoc-kit/utils/assert';
8+
} from '../../renderers/mobiledoc/0-3-2';
9+
10+
import { kvArrayToObject, filter } from '../../utils/array-utils';
11+
import assert from '../../utils/assert';
12+
import { entries } from '../../utils/object-utils';
1113

1214
/*
1315
* Parses from mobiledoc -> post
@@ -113,7 +115,7 @@ export default class MobiledocParser {
113115
const section = this.builder.createMarkupSection(tagName);
114116
post.sections.append(section);
115117
if (attributesArray) {
116-
Object.entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => {
118+
entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => {
117119
section.setAttribute(key, value);
118120
});
119121
}
@@ -129,7 +131,7 @@ export default class MobiledocParser {
129131
const section = this.builder.createListSection(tagName);
130132
post.sections.append(section);
131133
if (attributesArray) {
132-
Object.entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => {
134+
entries(kvArrayToObject(attributesArray)).forEach(([key, value]) => {
133135
section.setAttribute(key, value);
134136
});
135137
}

src/js/utils/object-utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function entries(obj) {
2+
const ownProps = Object.keys(obj);
3+
let i = ownProps.length;
4+
const resArray = new Array(i);
5+
6+
while (i--) {
7+
resArray[i] = [ownProps[i], obj[ownProps[i]]];
8+
}
9+
10+
return resArray;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Helpers from '../../test-helpers';
2+
import { entries } from 'mobiledoc-kit/utils/object-utils';
3+
4+
const { module, test } = Helpers;
5+
6+
module('Unit: Utils: Object Utils');
7+
8+
test('#entries works', assert => {
9+
assert.deepEqual(entries({ hello: 'world', goodbye: 'moon' }), [
10+
['hello', 'world'],
11+
['goodbye', 'moon']
12+
]);
13+
});

0 commit comments

Comments
 (0)