zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const unidecode = require('unidecode');
 
const INVALID_SEPARATOR_REGEXP = /[^-._~]/;
const TITLE_CASE_REGEXP = /(?:^| )[a-z]/g;
const CONVERT_REGEXP = /[^A-Za-z0-9]+|([a-z])([A-Z])/g;
const CONVERT_SEPARATOR_REGEXP = / /g;
const REVERT_REGEXP = {}; /* RegExp intances are based on the separator and then cached */
const REVERT_AUTO_REGEXP = /[-._~]+|([a-z])([A-Z])/g;
const REVERT_CAMEL_CASE_REGEXP = /([a-z])([A-Z])/g;
 
/**
 * Creates a new instance of url-slug
 */
function UrlSlug(separator, transform) {
 
    /* Set defaults */
 
    separator = null == separator ? '-' : separator;
    transform = null == transform ? 'lowercase' : transform;
 
    /* Validate through prepare method */
 
    var options = this.prepare(separator, transform);
 
    this.separator = options.separator;
    this.transform = options.transform;
 
}
 
/**
 * Builtin transformers
 */
UrlSlug.prototype.transformers = {
 
    lowercase: function (string) {
        return string.toLowerCase();
    },
 
    uppercase: function (string) {
        return string.toUpperCase();
    },
 
    titlecase: function (string) {
        return string.toLowerCase().replace(TITLE_CASE_REGEXP, function (character) {
            return character.toUpperCase();
        });
    },
 
};
 
/**
 * Check and return validated options
 */
UrlSlug.prototype.prepare = function (separator, transform) {
 
    if (null == separator) {
        separator = this.separator;
    } else if ('string' !== typeof separator) {
        throw new Error('Invalid separator, must be a string: "' + separator + '".');
    } else if (INVALID_SEPARATOR_REGEXP.test(separator)) {
        throw new Error('Invalid separator, has invalid characters: "' + separator + '".');
    }
 
    if (null == transform) {
        transform = this.transform;
    } else if (false === transform) {
        transform = false;
    } else if (this.transformers[transform]) {
        transform = this.transformers[transform];
    } else if ('function' !== typeof transform) {
        throw new Error('Invalid transform, must be a builtin transform or a function: "' + transform + '".');
    }
 
    return {
        separator: separator,
        transform: transform,
    }
 
}
 
/**
 * Converts a string into a slug
 */
UrlSlug.prototype.convert = function (string, separator, transform) {
 
    if ('string' !== typeof string) {
        throw new Error('Invalid value, must be a string: "' + string + '".');
    }
 
    options = this.prepare(separator, transform);
 
    /* Transliterate and replace invalid characters, then replace non alphanumeric characters with spaces */
 
    string = unidecode(string).replace('[?]', '').replace(CONVERT_REGEXP, '$1 $2').trim();
 
    /* Pass string through transform function */
 
    if (options.transform) {
        string = options.transform(string);
    }
 
    /* Replace spaces with separator and return */
 
    return string.replace(CONVERT_SEPARATOR_REGEXP, options.separator);
 
};
 
/**
 * Reverts a slug back to a string
 */
UrlSlug.prototype.revert = function (slug, separator, transform) {
 
    if ('string' !== typeof slug) {
        throw new Error('Invalid value, must be a string: "' + slug + '".');
    }
 
    options = this.prepare(separator, transform);
 
    /* Determine which regular expression will be used to—and—remove separators */
 
    if ('' === options.separator) {
        slug = slug.replace(REVERT_CAMEL_CASE_REGEXP, '$1 $2');
    } else if ('string' === typeof separator) {
        /* If separator argument was set as string, don't check options.separator,
        it can return the default separator (this.separator) */
        REVERT_REGEXP[separator] = REVERT_REGEXP[separator] || new RegExp('\\' + separator.split('').join('\\'), 'g');
        slug = slug.replace(REVERT_REGEXP[separator], ' ');
    } else {
        slug = slug.replace(REVERT_AUTO_REGEXP, '$1 $2');
    }
 
    /* Pass slug through transform function and return. Check if transform
    was set in arguments, to avoid using the default transform. */
 
    return transform && options.transform ? options.transform(slug) : slug;
 
};
 
/* Prepare the global instance and export it */
 
var urlSlug = new UrlSlug;
var globalInstance = urlSlug.convert.bind(urlSlug);
 
globalInstance.UrlSlug = UrlSlug;
globalInstance.convert = globalInstance;
globalInstance.revert = urlSlug.revert.bind(urlSlug);
 
module.exports = globalInstance;