Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for segments in Period and Representation. #19

Merged
merged 9 commits into from
Feb 26, 2018
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"version": "5.0.0"
},
"dependencies": {
"deepmerge": "^2.0.1",
"global": "^4.3.0",
"url-toolkit": "^2.1.1"
}
Expand Down
19 changes: 10 additions & 9 deletions src/inheritAttributes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { flatten } from './utils/list';
import { shallowMerge, getAttributes } from './utils/object';
import { getAttributes } from './utils/object';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can import merge along with getAttributes here

import { parseDuration } from './utils/time';
import { findChildren, getContent } from './utils/xml';
import resolveUrl from './utils/resolveUrl';
import errors from './errors';
import merge from 'deepmerge';
// import merge from 'deepmerge';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this comment

import { merge } from './utils/object';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can remove this line


/**
* Builds a list of urls that is the product of the reference urls and BaseURL values
Expand Down Expand Up @@ -53,7 +54,7 @@ export const getSegmentInformation = (adaptationSet) => {
const segmentTemplate = findChildren(adaptationSet, 'SegmentTemplate')[0];
const segmentList = findChildren(adaptationSet, 'SegmentList')[0];
const segmentUrls = segmentList && findChildren(segmentList, 'SegmentURL')
.map(s => shallowMerge({ tag: 'SegmentURL' }, getAttributes(s)));
.map(s => merge({ tag: 'SegmentURL' }, getAttributes(s)));
const segmentBase = findChildren(adaptationSet, 'SegmentBase')[0];
const segmentTimelineParentNode = segmentList || segmentTemplate;
const segmentTimeline = segmentTimelineParentNode &&
Expand Down Expand Up @@ -83,13 +84,13 @@ export const getSegmentInformation = (adaptationSet) => {
template,
timeline: segmentTimeline &&
findChildren(segmentTimeline, 'S').map(s => getAttributes(s)),
list: segmentList && shallowMerge(
list: segmentList && merge(
getAttributes(segmentList),
{
segmentUrls,
initialization: getAttributes(segmentInitialization)
}),
base: segmentBase && shallowMerge(
base: segmentBase && merge(
getAttributes(segmentBase), {
initialization: getAttributes(segmentInitialization)
})
Expand Down Expand Up @@ -143,13 +144,13 @@ export const inheritBaseUrls =
(adaptationSetAttributes, adaptationSetBaseUrls, adaptationSetSegmentInfo) => (representation) => {
const repBaseUrlElements = findChildren(representation, 'BaseURL');
const repBaseUrls = buildBaseUrls(adaptationSetBaseUrls, repBaseUrlElements);
const attributes = shallowMerge(adaptationSetAttributes, getAttributes(representation));
const attributes = merge(adaptationSetAttributes, getAttributes(representation));
const representationSegmentInfo = getSegmentInformation(representation);

return repBaseUrls.map(baseUrl => {
return {
segmentInfo: merge(adaptationSetSegmentInfo, representationSegmentInfo),
attributes: shallowMerge(attributes, { baseUrl })
attributes: merge(attributes, { baseUrl })
};
});
};
Expand Down Expand Up @@ -183,7 +184,7 @@ export const toRepresentations =
findChildren(adaptationSet, 'BaseURL'));
const role = findChildren(adaptationSet, 'Role')[0];
const roleAttributes = { role: getAttributes(role) };
const attrs = shallowMerge(periodAttributes,
const attrs = merge(periodAttributes,
adaptationSetAttributes,
roleAttributes);
const segmentInfo = getSegmentInformation(adaptationSet);
Expand Down Expand Up @@ -222,7 +223,7 @@ export const toRepresentations =
export const toAdaptationSets = (mpdAttributes, mpdBaseUrls) => (period, periodIndex) => {
const periodBaseUrls = buildBaseUrls(mpdBaseUrls, findChildren(period, 'BaseURL'));
const periodAtt = getAttributes(period);
const periodAttributes = shallowMerge(mpdAttributes, periodAtt, { periodIndex });
const periodAttributes = merge(mpdAttributes, periodAtt, { periodIndex });
const adaptationSets = findChildren(period, 'AdaptationSet');
const periodSegmentInfo = getSegmentInformation(period);

Expand Down
8 changes: 4 additions & 4 deletions src/toPlaylists.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { shallowMerge } from './utils/object';
import { merge } from './utils/object';
import { segmentsFromTemplate } from './segment/segmentTemplate';
import { segmentsFromList } from './segment/segmentList';
import { segmentsFromBase } from './segment/segmentBase';
// import merge from 'deepmerge';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leftover comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont forget to remove this comment

export const generateSegments = (segmentInfo, attributes) => {
if (segmentInfo.template) {
return segmentsFromTemplate(
shallowMerge(attributes, segmentInfo.template),
merge(attributes, segmentInfo.template),
segmentInfo.timeline
);
}
if (segmentInfo.base) {
return segmentsFromBase(shallowMerge(attributes, segmentInfo.base));
return segmentsFromBase(merge(attributes, segmentInfo.base));
}
if (segmentInfo.list) {
return segmentsFromList(
shallowMerge(attributes, segmentInfo.list), segmentInfo.timeline
merge(attributes, segmentInfo.list), segmentInfo.timeline
);
}
};
Expand Down
23 changes: 17 additions & 6 deletions src/utils/object.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { from } from './list';

export const shallowMerge = (...objects) => {
export const merge = (...objects) => {

const isObject = (obj) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function can be defined outside of merge

return obj && typeof obj === 'object';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets be explicit and use !!obj

};

return objects.reduce((x, y) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to update this to use more descriptive names for readability. maybe result for x and source for y

return Object.keys(y)
.reduce((o, key) => {
o[key] = y[key];

return o;
}, x);
Object.keys(y).forEach(key => {

if (Array.isArray(x[key]) && Array.isArray(y[key])) {
x[key] = x[key].concat(y[key]);
} else if (isObject(x[key]) && isObject(y[key])) {
x[key] = merge(x[key], y[key]);
} else {
x[key] = y[key];
}
});
return x;
}, {});
};

Expand Down
20 changes: 10 additions & 10 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallowMerge, getAttributes } from '../src/utils/object';
import { getAttributes, merge } from '../src/utils/object';
import { parseDuration } from '../src/utils/time';
import { flatten, range, from } from '../src/utils/list';
import { findChildren, getContent } from '../src/utils/xml';
Expand All @@ -8,25 +8,25 @@ import QUnit from 'qunit';

QUnit.module('utils');

QUnit.module('shallowMerge');
QUnit.module('merge');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to add a test or two for the new merge functionality

QUnit.test('empty', function(assert) {
assert.deepEqual(shallowMerge({}, { a: 1 }), { a: 1 });
assert.deepEqual(shallowMerge({ a: 1 }, { a: 1 }), { a: 1 });
assert.deepEqual(shallowMerge({ a: 1 }, {}), { a: 1 });
assert.deepEqual(merge({}, { a: 1 }), { a: 1 });
assert.deepEqual(merge({ a: 1 }, { a: 1 }), { a: 1 });
assert.deepEqual(merge({ a: 1 }, {}), { a: 1 });
});

QUnit.test('append', function(assert) {
assert.deepEqual(shallowMerge({ a: 1 }, { b: 3 }), { a: 1, b: 3 });
assert.deepEqual(merge({ a: 1 }, { b: 3 }), { a: 1, b: 3 });
});

QUnit.test('overwrite', function(assert) {
assert.deepEqual(shallowMerge({ a: 1 }, { a: 2 }), { a: 2 });
assert.deepEqual(merge({ a: 1 }, { a: 2 }), { a: 2 });
});

QUnit.test('empty', function(assert) {
assert.deepEqual(shallowMerge({}, {}), {});
assert.deepEqual(shallowMerge({}, 1), {});
assert.deepEqual(shallowMerge(1, {}), {});
assert.deepEqual(merge({}, {}), {});
assert.deepEqual(merge({}, 1), {});
assert.deepEqual(merge(1, {}), {});
});

QUnit.module('flatten');
Expand Down