PHP's gopher_parsedir in JavaScript

Here’s what our current JavaScript equivalent to PHP's gopher_parsedir 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
module.exports = function gopher_parsedir (dirent) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/gopher_parsedir/
// original by: Brett Zamir (http://brett-zamir.me)
// example 1: var entry = gopher_parsedir('0All about my gopher site.\t/allabout.txt\tgopher.example.com\t70\u000d\u000a')
// example 1: entry.title
// returns 1: 'All about my gopher site.'
/* Types
* 0 = plain text file
* 1 = directory menu listing
* 2 = CSO search query
* 3 = error message
* 4 = BinHex encoded text file
* 5 = binary archive file
* 6 = UUEncoded text file
* 7 = search engine query
* 8 = telnet session pointer
* 9 = binary file
* g = Graphics file format, primarily a GIF file
* h = HTML file
* i = informational message
* s = Audio file format, primarily a WAV file
*/
var entryPattern = /^(.)(.*?)\t(.*?)\t(.*?)\t(.*?)\u000d\u000a$/
var entry = dirent.match(entryPattern)
if (entry === null) {
throw new Error('Could not parse the directory entry')
// return false;
}
var type = entry[1]
switch (type) {
case 'i':
// GOPHER_INFO
type = 255
break
case '1':
// GOPHER_DIRECTORY
type = 1
break
case '0':
// GOPHER_DOCUMENT
type = 0
break
case '4':
// GOPHER_BINHEX
type = 4
break
case '5':
// GOPHER_DOSBINARY
type = 5
break
case '6':
// GOPHER_UUENCODED
type = 6
break
case '9':
// GOPHER_BINARY
type = 9
break
case 'h':
// GOPHER_HTTP
type = 254
break
default:
return {
type: -1,
data: dirent
} // GOPHER_UNKNOWN
}
return {
type: type,
title: entry[2],
path: entry[3],
host: entry[4],
port: entry[5]
}
}
[ 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/net-gopher/gopher_parsedir'). You could also require the net-gopher module in full so that you could access net-gopher.gopher_parsedir 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.

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
1var entry = gopher_parsedir('0All about my gopher site.\t/allabout.txt\tgopher.example.com\t70\u000d\u000a') entry.title'All about my gopher site.'

« More PHP net-gopher functions