Skip to content

Instantly share code, notes, and snippets.

@abhi-bit
Last active September 23, 2015 14:35
Show Gist options
  • Save abhi-bit/dde6c00b9037f89963f6 to your computer and use it in GitHub Desktop.
Save abhi-bit/dde6c00b9037f89963f6 to your computer and use it in GitHub Desktop.
map function
function (doc, meta) {
if (meta.type != "json") return;
var stringToUtf8Bytes = function (str) {
var utf8 = unescape(encodeURIComponent(str));
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
};
var indexFormattedValue = function (val) {
console.log("val", val);
console.log("typeof", typeof val);
if (val === null) {
return [64];
} else if (typeof val == "boolean") {
return [96, val];
} else if (typeof val == "number") {
return [128, val];
} else if (typeof val == "string") {
return [160, stringToUtf8Bytes(val)];
} else if (typeof val == "object") {
if (val instanceof Array) {
return [192, val];
} else {
innerKeys = [];
for (var k in val) {
innerKeys.push(k);
}
innerKeys.sort()
innerVals = [];
for (var i in innerKeys) {
console.log("key", innerKeys[i]);
console.log("typeof", typeof val[innerKeys[i]]);
if (typeof val[innerKeys[i]] == "object") {
if (Object.keys(val[innerKeys[i]]).length === 0) {
innerVals.push([224, [[], []]]);
}
} else {
innerVals.push(indexFormattedValue(val[innerKeys[i]]));i
}
}
return [224, [innerKeys, innerVals]];
}
} else {
return undefined;
}
};
var key1 = indexFormattedValue(doc.item.header.name);
var key = [key1];
var pos = key.indexOf(undefined);
if (pos == 0) {
return;
} else if (pos > 0) {
key.splice(pos)
}
if (doc._meta.docType == "policy") {
emit(key, null);
}
}
// salt: 1263034472
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment