mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-05-30 01:47:17 +09:00
Refactor: Extract utility methods
This commit is contained in:
parent
f2ea760806
commit
e5c2672171
@ -1,4 +1,5 @@
|
|||||||
import * as URL from 'url';
|
import * as URL from 'url';
|
||||||
|
import nullOrEmpty from './utils/null-or-empty';
|
||||||
|
|
||||||
const escapeRegExp = require('escape-regexp');
|
const escapeRegExp = require('escape-regexp');
|
||||||
|
|
||||||
@ -91,29 +92,3 @@ export default async (url: URL.Url): Promise<ISummary> => {
|
|||||||
sitename: siteName
|
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
15
src/utils/clip.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
11
src/utils/null-or-empty.ts
Normal file
11
src/utils/null-or-empty.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user