Skip to content

Commit

Permalink
Objectの型検証及び値取得をconfigureからcommonに分離
Browse files Browse the repository at this point in the history
  • Loading branch information
h-sugawara committed Nov 21, 2023
1 parent 53e0ce7 commit 7ece962
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
10 changes: 10 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ function hasProperty(obj, key) {
return obj && Object.hasOwnProperty.call(obj, key);
}

function hasTypeOfProperty(obj, key, type) {
return hasProperty(obj, key) && typeof obj[key] === type;
}

function getObjectValueFrom(obj, key, type, defaultValue) {
return hasTypeOfProperty(obj, key, type) ? obj[key] || defaultValue : defaultValue;
}

function stringLength(text) {
if (typeof text !== 'string') {
throw new Error('text is not string type.');
Expand Down Expand Up @@ -49,6 +57,8 @@ function stringSlice(text, start, end) {

module.exports = {
hasProperty,
hasTypeOfProperty,
getObjectValueFrom,
stringLength,
stringSlice,
};
14 changes: 6 additions & 8 deletions lib/configure.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { hasProperty } = require('./common');
const { hasProperty, hasTypeOfProperty, getObjectValueFrom } = require('./common');

const ANCHOR_LINK_CLASS_NAME = 'link-preview';
const DESCRIPTION_LENGTH = 140;
Expand All @@ -12,9 +12,7 @@ function getClassName(classNameFromHexoCfg, defaultAnchorLink) {
if (typeof classNameFromHexoCfg === 'string') {
obj.anchor_link = classNameFromHexoCfg || obj.anchor_link;
} else if (typeof classNameFromHexoCfg === 'object') {
if (hasProperty(classNameFromHexoCfg, 'anchor_link') && typeof classNameFromHexoCfg.anchor_link === 'string') {
obj.anchor_link = classNameFromHexoCfg.anchor_link || obj.anchor_link;
}
obj.anchor_link = getObjectValueFrom(classNameFromHexoCfg, 'anchor_link', 'string', obj.anchor_link);

if (hasProperty(classNameFromHexoCfg, 'image') && classNameFromHexoCfg.image !== '') {
obj.image = classNameFromHexoCfg.image;
Expand All @@ -40,10 +38,10 @@ module.exports = hexoCfg => {
if (hasProperty(hexoCfgLinkPreview, 'class_name')) {
config.class_name = getClassName(hexoCfgLinkPreview.class_name, config.class_name.anchor_link);
}
if (hasProperty(hexoCfgLinkPreview, 'description_length') && typeof hexoCfgLinkPreview.description_length === 'number') {
config.description_length = hexoCfgLinkPreview.description_length || config.description_length;
}
if (hasProperty(hexoCfgLinkPreview, 'disguise_crawler') && typeof hexoCfgLinkPreview.disguise_crawler === 'boolean') {

config.description_length = getObjectValueFrom(hexoCfgLinkPreview, 'description_length', 'number', config.description_length);

if (hasTypeOfProperty(hexoCfgLinkPreview, 'disguise_crawler', 'boolean')) {
config.disguise_crawler = hexoCfgLinkPreview.disguise_crawler;
}

Expand Down
12 changes: 6 additions & 6 deletions test/configure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ describe('configure', () => {
it('Specify all values', () => {
const hexoCfg = {
link_preview: {
class_name: { anchor_link: 'link-preview', image: 'not-gallery-item' },
class_name: { anchor_link: 'link-preview-2', image: 'not-gallery-item' },
description_length: 100,
disguise_crawler: false,
},
};

expect(getConfig(hexoCfg)).toEqual(
{
class_name: { anchor_link: 'link-preview', image: 'not-gallery-item' },
class_name: { anchor_link: 'link-preview-2', image: 'not-gallery-item' },
description_length: 100,
disguise_crawler: false,
}
Expand All @@ -36,13 +36,13 @@ describe('configure', () => {
it('Specify a valid string value at class_name', () => {
const hexoCfg = {
link_preview: {
class_name: 'link-preview',
class_name: 'link-preview-2',
},
};

expect(getConfig(hexoCfg)).toEqual(
{
class_name: { anchor_link: 'link-preview' },
class_name: { anchor_link: 'link-preview-2' },
description_length: 140,
disguise_crawler: true,
}
Expand All @@ -68,14 +68,14 @@ describe('configure', () => {
it('Specify a object which has valid anchor_link only at class_name', () => {
const hexoCfg = {
link_preview: {
class_name: { anchor_link: 'link-preview' },
class_name: { anchor_link: 'link-preview-2' },
},
};

expect(getConfig(hexoCfg)).toEqual(
{
class_name: {
anchor_link: 'link-preview',
anchor_link: 'link-preview-2',
},
description_length: 140,
disguise_crawler: true,
Expand Down

0 comments on commit 7ece962

Please sign in to comment.