-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
test.js
58 lines (52 loc) · 3.13 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import filenamify, {filenamifyPath} from './index.js';
const directoryName = path.dirname(fileURLToPath(import.meta.url));
test('filenamify()', t => {
t.is(filenamify('foo/bar'), 'foo!bar');
t.is(filenamify('foo//bar'), 'foo!bar');
t.is(filenamify('//foo//bar//'), '!foo!bar!');
t.is(filenamify('foo\\\\\\bar'), 'foo!bar');
t.is(filenamify('foo/bar', {replacement: '🐴🐴'}), 'foo🐴🐴bar');
t.is(filenamify('////foo////bar////', {replacement: '(('}), '((foo((bar((');
t.is(filenamify('foo\u0000bar'), 'foo!bar');
t.is(filenamify('.'), '!');
t.is(filenamify('..'), '!');
t.is(filenamify('./'), '!');
t.is(filenamify('../'), '!');
t.is(filenamify('!.foo'), '!.foo');
t.is(filenamify('foo.!'), 'foo.!');
t.is(filenamify('foo.bar.'), 'foo.bar');
t.is(filenamify('foo.bar..'), 'foo.bar');
t.is(filenamify('foo.bar...'), 'foo.bar');
t.is(filenamify('con'), 'con!');
t.is(filenamify('foo/bar/nul'), 'foo!bar!nul');
t.is(filenamify('con', {replacement: '🐴🐴'}), 'con🐴🐴');
t.is(filenamify('c/n', {replacement: 'o'}), 'cono');
t.is(filenamify('c/n', {replacement: 'con'}), 'cconn');
t.is(filenamify('.dotfile'), '.dotfile');
t.is(filenamify('my <file name-', {replacement: '-'}), 'my -file name-');
t.is(filenamify('--<abc->>>--', {replacement: '-'}), '---abc----');
t.is(filenamify('-<<abc>-', {replacement: '-'}), '--abc--');
t.is(filenamify('my <file name<', {replacement: '-'}), 'my -file name-');
t.is(filenamify('my <file name<'), 'my !file name!');
});
test('filenamifyPath()', t => {
t.is(path.basename(filenamifyPath(path.join(directoryName, 'foo:bar'))), 'foo!bar');
t.is(path.basename(filenamifyPath(path.join(directoryName, 'This? This is very long filename that will lose its extension when passed into filenamify, which could cause issues.csv'))),
'This! This is very long filename that will lose its extension when passed into filenamify, which.csv');
});
test('filenamify length', t => {
// Basename length: 152
const filename = 'this/is/a/very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt';
t.is(filenamify(path.basename(filename)), 'very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_v.txt');
t.is(filenamify(path.basename(filename), {maxLength: 180}), 'very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename.txt');
// File extension longer than `maxLength`
t.is(filenamify('foo.asdfghjkl', {maxLength: 5}), 'f.asdfghjkl');
// Basename length: 148
const filenameNoExt = 'very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_long_filename';
t.is(filenamify(filenameNoExt), 'very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_very_');
t.is(filenamify(filenameNoExt, {maxLength: 20}), 'very_very_very_very_');
t.is(filenamify('.asdfghjkl', {maxLength: 2}), '.asdfghjkl');
});