PHP's convert_uuencode in JavaScript

Here’s what our current JavaScript equivalent to PHP's convert_uuencode 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
module.exports = function convert_uuencode (str) { // eslint-disable-line camelcase
// discuss at: http://locutus.io/php/convert_uuencode/
// original by: Ole Vrijenhoek
// bugfixed by: Kevin van Zonneveld (http://kvz.io)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// reimplemented by: Ole Vrijenhoek
// example 1: convert_uuencode("test\ntext text\r\n")
// returns 1: "0=&5S=`IT97AT('1E>'0-\"@\n`\n"
var isScalar = require('../var/is_scalar')
var chr = function (c) {
return String.fromCharCode(c)
}
if (!str || str === '') {
return chr(0)
} else if (!isScalar(str)) {
return false
}
var c = 0
var u = 0
var i = 0
var a = 0
var encoded = ''
var tmp1 = ''
var tmp2 = ''
var bytes = {}
// divide string into chunks of 45 characters
var chunk = function () {
bytes = str.substr(u, 45).split('')
for (i in bytes) {
bytes[i] = bytes[i].charCodeAt(0)
}
return bytes.length || 0
}
while ((c = chunk()) !== 0) {
u += 45
// New line encoded data starts with number of bytes encoded.
encoded += chr(c + 32)
// Convert each char in bytes[] to a byte
for (i in bytes) {
tmp1 = bytes[i].toString(2)
while (tmp1.length < 8) {
tmp1 = '0' + tmp1
}
tmp2 += tmp1
}
while (tmp2.length % 6) {
tmp2 = tmp2 + '0'
}
for (i = 0; i <= (tmp2.length / 6) - 1; i++) {
tmp1 = tmp2.substr(a, 6)
if (tmp1 === '000000') {
encoded += chr(96)
} else {
encoded += chr(parseInt(tmp1, 2) + 32)
}
a += 6
}
a = 0
tmp2 = ''
encoded += '\n'
}
// Add termination characters
encoded += chr(96) + '\n'
return encoded
}
[ 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/strings/convert_uuencode'). You could also require the strings module in full so that you could access strings.convert_uuencode 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
1convert_uuencode("test\ntext text\r\n")"0=&5S=`IT97AT('1E>'0-\"@\n`\n"

« More PHP strings functions