Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pgutil.js 1.3 KB

You have to be logged in to leave a comment. Sign In
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
  1. const stream = require('stream');
  2. function escape(txt) {
  3. let buf = Buffer.isBuffer(txt) ? txt : Buffer.from(txt, 'utf-8');
  4. const n = buf.length;
  5. let tgt = Buffer.allocUnsafe(n * 2);
  6. let opos = 0;
  7. for (let i = 0; i < n; i++) {
  8. let b = buf[i];
  9. switch (b) {
  10. case 92: // backslash
  11. tgt[opos++] = 92;
  12. tgt[opos++] = 92;
  13. break;
  14. case 9: // tab
  15. tgt[opos++] = 92;
  16. tgt[opos++] = 116;
  17. break;
  18. case 10: // newline
  19. tgt[opos++] = 92;
  20. tgt[opos++] = 110;
  21. break;
  22. case 13: // carriage return
  23. tgt[opos++] = 92;
  24. tgt[opos++] = 114;
  25. break;
  26. default:
  27. tgt[opos++] = b;
  28. }
  29. }
  30. return tgt.slice(0, opos);
  31. }
  32. module.exports.escapePGText = escape;
  33. class PGEncode extends stream.Transform {
  34. constructor() {
  35. super({objectMode: true});
  36. }
  37. _write(rec, enc, cb) {
  38. let n = rec.length;
  39. for (let i = 0; i < n; i++) {
  40. let f = rec[i];
  41. if (i > 0) this.push('\t');
  42. if (f == null) {
  43. this.push('\\N');
  44. } else if (typeof f == 'string' || f instanceof Buffer) {
  45. this.push(escape(f));
  46. } else {
  47. this.push(escape(f.toString()));
  48. }
  49. }
  50. this.push('\n');
  51. cb();
  52. }
  53. }
  54. module.exports.encodePGText = function() {
  55. return new PGEncode();
  56. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...