PHP's pathinfo in JavaScript

Here’s what our current JavaScript equivalent to PHP's pathinfo looks like.

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
149
150
151
module.exports = function pathinfo (path, options) {
// discuss at: http://locutus.io/php/pathinfo/
// original by: Nate
// revised by: Kevin van Zonneveld (http://kvz.io)
// improved by: Brett Zamir (http://brett-zamir.me)
// improved by: Dmitry Gorelenkov
// input by: Timo
// note 1: Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559
// note 1: The way the bitwise arguments are handled allows for greater flexibility
// note 1: & compatability. We might even standardize this
// note 1: code and use a similar approach for
// note 1: other bitwise PHP functions
// note 1: Locutus tries very hard to stay away from a core.js
// note 1: file with global dependencies, because we like
// note 1: that you can just take a couple of functions and be on your way.
// note 1: But by way we implemented this function,
// note 1: if you want you can still declare the PATHINFO_*
// note 1: yourself, and then you can use:
// note 1: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION);
// note 1: which makes it fully compliant with PHP syntax.
// example 1: pathinfo('/www/htdocs/index.html', 1)
// returns 1: '/www/htdocs'
// example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME')
// returns 2: 'index.html'
// example 3: pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION')
// returns 3: 'html'
// example 4: pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME')
// returns 4: 'index'
// example 5: pathinfo('/www/htdocs/index.html', 2 | 4)
// returns 5: {basename: 'index.html', extension: 'html'}
// example 6: pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL')
// returns 6: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
// example 7: pathinfo('/www/htdocs/index.html')
// returns 7: {dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
var basename = require('../filesystem/basename')
var opt = ''
var realOpt = ''
var optName = ''
var optTemp = 0
var tmpArr = {}
var cnt = 0
var i = 0
var haveBasename = false
var haveExtension = false
var haveFilename = false
// Input defaulting & sanitation
if (!path) {
return false
}
if (!options) {
options = 'PATHINFO_ALL'
}
// Initialize binary arguments. Both the string & integer (constant) input is
// allowed
var OPTS = {
'PATHINFO_DIRNAME': 1,
'PATHINFO_BASENAME': 2,
'PATHINFO_EXTENSION': 4,
'PATHINFO_FILENAME': 8,
'PATHINFO_ALL': 0
}
// PATHINFO_ALL sums up all previously defined PATHINFOs (could just pre-calculate)
for (optName in OPTS) {
if (OPTS.hasOwnProperty(optName)) {
OPTS.PATHINFO_ALL = OPTS.PATHINFO_ALL | OPTS[optName]
}
}
if (typeof options !== 'number') {
// Allow for a single string or an array of string flags
options = [].concat(options)
for (i = 0; i < options.length; i++) {
// Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
if (OPTS[options[i]]) {
optTemp = optTemp | OPTS[options[i]]
}
}
options = optTemp
}
// Internal Functions
var _getExt = function (path) {
var str = path + ''
var dotP = str.lastIndexOf('.') + 1
return !dotP ? false : dotP !== str.length ? str.substr(dotP) : ''
}
// Gather path infos
if (options & OPTS.PATHINFO_DIRNAME) {
var dirName = path
.replace(/\\/g, '/')
.replace(/\/[^/]*\/?$/, '') // dirname
tmpArr.dirname = dirName === path ? '.' : dirName
}
if (options & OPTS.PATHINFO_BASENAME) {
if (haveBasename === false) {
haveBasename = basename(path)
}
tmpArr.basename = haveBasename
}
if (options & OPTS.PATHINFO_EXTENSION) {
if (haveBasename === false) {
haveBasename = basename(path)
}
if (haveExtension === false) {
haveExtension = _getExt(haveBasename)
}
if (haveExtension !== false) {
tmpArr.extension = haveExtension
}
}
if (options & OPTS.PATHINFO_FILENAME) {
if (haveBasename === false) {
haveBasename = basename(path)
}
if (haveExtension === false) {
haveExtension = _getExt(haveBasename)
}
if (haveFilename === false) {
haveFilename = haveBasename.slice(0, haveBasename.length - (haveExtension
? haveExtension.length + 1
: haveExtension === false
? 0
: 1
)
)
}
tmpArr.filename = haveFilename
}
// If array contains only 1 element: return string
cnt = 0
for (opt in tmpArr) {
if (tmpArr.hasOwnProperty(opt)) {
cnt++
realOpt = opt
}
}
if (cnt === 1) {
return tmpArr[realOpt]
}
// Return full-blown array
return tmpArr
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/filesystem/pathinfo'). You could also require the filesystem module in full so that you could access filesystem.pathinfo instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Inspired by actual PHP source: php5-5.2.6/ext/standard/string.c line #1559 The way the bitwise arguments are handled allows for greater flexibility & compatability. We might even standardize this code and use a similar approach for other bitwise PHP functions Locutus tries very hard to stay away from a core.js file with global dependencies, because we like that you can just take a couple of functions and be on your way. But by way we implemented this function, if you want you can still declare the PATHINFO_* yourself, and then you can use: pathinfo(‘/www/index.html’, PATHINFO_BASENAME | PATHINFO_EXTENSION); which makes it fully compliant with PHP syntax.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1pathinfo('/www/htdocs/index.html', 1)'/www/htdocs'
2pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME')'index.html'
3pathinfo('/www/htdocs/index.html', 'PATHINFO_EXTENSION')'html'
4pathinfo('/www/htdocs/index.html', 'PATHINFO_FILENAME')'index'
5pathinfo('/www/htdocs/index.html', 2 | 4){basename: 'index.html', extension: 'html'}
6pathinfo('/www/htdocs/index.html', 'PATHINFO_ALL'){dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}
7pathinfo('/www/htdocs/index.html'){dirname: '/www/htdocs', basename: 'index.html', extension: 'html', filename: 'index'}

« More PHP filesystem functions