Skip to content

Commit

Permalink
refactor: getConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tsi committed Aug 24, 2023
1 parent d81f2f2 commit de24bb3
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"dependencies": {
"@cloudinary/url-gen": "^1.10.2",
"dashjs": "^4.7.1",
"lodash": "^4.17.21",
"uuid": "^9.0.0",
"video.js": "8.3.0",
"videojs-contrib-ads": "^6.9.0",
Expand Down
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'assets/styles/main.scss';
import VideoPlayer from './video-player';
import { assign } from 'utils/assign';
import { omit, pick, convertKeysToSnakeCase } from './utils/object';
import { pick, convertKeysToSnakeCase } from './utils/object';
import { CLOUDINARY_CONFIG_PARAM } from './video-player.const';

if (window.cloudinary && window.cloudinary.Cloudinary) {
Expand All @@ -10,12 +10,14 @@ if (window.cloudinary && window.cloudinary.Cloudinary) {
);
}

const getConfig = (playerOptions = {}, cloudinaryConfig) =>
const getConfig = (playerOptions = {}, cloudinaryConfig) => {
const snakeCaseCloudinaryConfig = pick(convertKeysToSnakeCase(playerOptions), CLOUDINARY_CONFIG_PARAM);

// pick cld-configurations and assign them to cloudinaryConfig
assign(omit(playerOptions, CLOUDINARY_CONFIG_PARAM), {
cloudinaryConfig:
cloudinaryConfig || convertKeysToSnakeCase(pick(playerOptions, CLOUDINARY_CONFIG_PARAM))
return assign(playerOptions, {
cloudinaryConfig: cloudinaryConfig || snakeCaseCloudinaryConfig
});
};

const getVideoPlayer = config => (id, playerOptions, ready) =>
new VideoPlayer(id, getConfig(playerOptions, config), ready);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelToSnake } from './string';
import snakeCase from 'lodash/snakeCase';

/**
* a nested value from an object
Expand Down Expand Up @@ -54,7 +54,7 @@ export const convertKeysToSnakeCase = (obj) => {
let snakeCaseObj = {};

Object.keys(obj).forEach((key) => {
const snakeCaseKey = camelToSnake(key);
const snakeCaseKey = snakeCase(key);
snakeCaseObj[snakeCaseKey] = obj[key];
});

Expand Down
8 changes: 1 addition & 7 deletions src/utils/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ function startsWith(str, searchStr, position) {
}
}

function camelToSnake(camelCaseString) {
return camelCaseString.replace(/[A-Z]/g, function (match) {
return '_' + match.toLowerCase();
});
}

export { camelize, startsWith, camelToSnake };
export { camelize, startsWith };
4 changes: 1 addition & 3 deletions src/video-player.const.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import videojs from 'video.js';
import { camelize } from './utils/string';

export const CLOUDINARY_PARAMS = [
'cloudinaryConfig',
Expand Down Expand Up @@ -51,8 +50,7 @@ export const CLOUDINARY_CONFIG_PARAM = [
'url_suffix',
'use_root_path',
'auth_token'
// Support camelCase as well
].map((param) => ([param, camelize(param)])).flat();
];

export const DEFAULT_HLS_OPTIONS = {
html5: {
Expand Down
8 changes: 4 additions & 4 deletions src/video-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class VideoPlayer extends Utils.mixin(Eventable) {
};
}

get playerOptions() {
return this.options.playerOptions;
}

constructor(elem, initOptions, ready) {
super();

Expand Down Expand Up @@ -516,10 +520,6 @@ class VideoPlayer extends Utils.mixin(Eventable) {
return this.videojs.cloudinary.cloudinaryConfig(config);
}

get playerOptions() {
return this.options.playerOptions;
}

currentPublicId() {
return this.videojs.cloudinary.currentPublicId();
}
Expand Down

0 comments on commit de24bb3

Please sign in to comment.