aboutsummaryrefslogtreecommitdiffstats
path: root/parseLDIF.js
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2013-05-13 15:48:02 +0200
committerMatěj Cepl <mcepl@redhat.com>2013-05-13 21:27:37 +0200
commitf23003947b2b34644cdedf9fd9699d8173b20ac7 (patch)
tree04fcd591a2097f65a4f64f2a51c8cee6a3625acb /parseLDIF.js
parent22afe0326408b03551b655aee52d151cef442b9e (diff)
downloadparseLDIF-f23003947b2b34644cdedf9fd9699d8173b20ac7.tar.gz
Add a simple testsuite.
Example files from RFC2849
Diffstat (limited to 'parseLDIF.js')
-rw-r--r--parseLDIF.js95
1 files changed, 74 insertions, 21 deletions
diff --git a/parseLDIF.js b/parseLDIF.js
index 286c573..3e932c1 100644
--- a/parseLDIF.js
+++ b/parseLDIF.js
@@ -1,4 +1,4 @@
-// Two compatibility shims
+// Compatibility shims
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
@@ -11,6 +11,14 @@ if (!String.prototype.trimRight) {
};
}
+if (!Array.isArray) {
+ Array.isArray = function (vArg) {
+ return Object.prototype.toString.call(vArg) === "[object Array]";
+ };
+}
+
+const debugState = false;
+
/**
*
* Base64 encode / decode
@@ -160,9 +168,15 @@ const usefulFields = ["birthyear", "c", "cn", "description",
"mobile", "mozillaHomeCountryName", "mozillaHomeLocalityName",
"mozillaHomePostalCode", "mozillaHomeState", "mozillaHomeStreet",
"mozillaHomeUrl", "mozillaNickname", "o", "sn", "st", "street",
- "telephoneNumber", "title"
+ "telephoneNumber", "title", "givenname", "objectclass"
];
+function debug(str) {
+ if (debugState) {
+ print(str);
+ }
+}
+
/**
* parse LDIF string into JavaScript Object
*
@@ -178,18 +192,64 @@ const usefulFields = ["birthyear", "c", "cn", "description",
* - ignores < links
*/
function parseLDIF(inStr) {
- var record = {}, key, value, splitLine, colon_idx,
+ var record = {},
+ key = "",
+ value = null,
+ splitLine = [],
+ colon_idx = 0,
out_records = [];
+ function handleAdding(key, value) {
+ value = value ? value.trim() : "";
+
+ // base64 encoded value
+ if (value[0] === ":") {
+ value = Base64.decode(value.slice(1).trim());
+ }
+
+ if (key && (usefulFields.indexOf(key) != -1) &&
+ value.length > 0) {
+ if (key in this) {
+ if (Array.isArray(this[key])) {
+ this[key].push(value);
+ }
+ else {
+ this[key] = new Array(this[key]);
+ this[key].push(value);
+ }
+ }
+ else {
+ this[key] = value;
+ }
+ }
+
+ key = "";
+ value = null;
+ }
+
+ record.add = handleAdding;
+
inStr.forEach(function (line) {
if (line != undefined) {
line = line.trim();
- if (line.length === 0) {
- if (Object.keys(record).length > 0) {
- out_records.push(record);
- record = {};
+ if (line.length == 0) {
+ // > 1, because we have always .add property
+ if (Object.keys(record).length > 1) {
+ record.add(key, value);
+ delete record.add;
+
+ if (record.objectclass &&
+ (record.objectclass === "person" ||
+ record.objectclass.indexOf("person") !== -1)) {
+ delete record.objectclass;
+ out_records.push(record);
+ }
}
+ record = {};
+ record.add = handleAdding;
+ key = "";
+ value = null;
}
else {
// comment line
@@ -208,26 +268,19 @@ function parseLDIF(inStr) {
// should be at least compatible for reading of RFC LDIF
// files (so it doesn’t hurt)
if (colon_idx == -1) {
- // TODO
- // The question is whether we shouldn't trim
- // the result of .decode() function as well.
- record[key] += Base64.decode(line).trim();
- return;
+ // multiline value
+ if (line[0] === " ") {
+ line = line.slice(1);
+ }
+ value += line;
}
else {
+ record.add(key, value);
+
key = line.slice(0, colon_idx);
value = line.slice(colon_idx + 1);
-
- // base64 encoded value
- if (value[0] === ":") {
- value = Base64.decode(value.slice(1));
- }
}
- if ((usefulFields.indexOf(key) != -1) &&
- value.length > 0) {
- record[key] = value.trim();
- }
}
}
}