Refactor: Extract utility methods

This commit is contained in:
syuilo 2017-01-31 00:33:38 +09:00
parent f2ea760806
commit e5c2672171
3 changed files with 27 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import * as URL from 'url';
import nullOrEmpty from './utils/null-or-empty';
const escapeRegExp = require('escape-regexp');
@ -91,29 +92,3 @@ export default async (url: URL.Url): Promise<ISummary> => {
sitename: siteName
};
};
function nullOrEmpty(val: string): boolean {
if (val === undefined) {
return true;
} else if (val === null) {
return true;
} else if (val.trim() === '') {
return true;
} else {
return false;
}
}
function clip(s: string, max: number): string {
if (nullOrEmpty(s)) {
return s;
}
s = s.trim();
if (s.length > max) {
return s.substr(0, max) + '...';
} else {
return s;
}
}

15
src/utils/clip.ts Normal file
View File

@ -0,0 +1,15 @@
import nullOrEmpty from './null-or-empty';
export default function(s: string, max: number) {
if (nullOrEmpty(s)) {
return s;
}
s = s.trim();
if (s.length > max) {
return s.substr(0, max) + '...';
} else {
return s;
}
}

View File

@ -0,0 +1,11 @@
export default function(val: string) {
if (val === undefined) {
return true;
} else if (val === null) {
return true;
} else if (val.trim() === '') {
return true;
} else {
return false;
}
}