Skip to content

Commit

Permalink
Starting to work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Stanton committed Jul 11, 2015
1 parent 4e6ec3a commit 0496b40
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 37 deletions.
68 changes: 31 additions & 37 deletions kynumbers/kynumbers.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
// Copyright 2015, Michael Stanton.
var KYNumbers = (function() {
var xmldoc = require('./xmldoc');
var fs = require("fs");
var contents = fs.readFileSync("./kynumbers.xml");
var data_doc = new xmldoc.XmlDocument(contents);

var FEATURE = {
SOUL: { value: 0, name: "SOUL" },
KARMA: { value: 1, name: "KARMA" },
GIFT: { value: 2, name: "GIFT" },
DESTINY:{ value: 3, name: "DESTINY" },
PATH: { value: 4, name: "PATH" }
SOUL: { value: 0, name: "soul" },
KARMA: { value: 1, name: "karma" },
GIFT: { value: 2, name: "gift" },
DESTINY:{ value: 3, name: "destiny" },
PATH: { value: 4, name: "path" }
};

var NUMBERS = [
"The first Spiritual Body is the Soul: it is represented by Guru " +
"Nanak and Guru Nanak represented Humility. " +
"The negative aspect of the number 1 is non-creativity. A person " +
"with a 1 in a negative position will have zero creativity. This " +
"is the most negative aspect of the number 1. He will be " +
"completely head-dominant.",

"The key phrase for the number 2 is 'Longing to Belong.' In order " +
"to make this number harmonize you have to connect with your " +
"spiritual teacher. This body is represented by Guru Angad - " +
"Obedience." +
"The negative aspect of the number 2 is that this person will form " +
"negative associations or he will be unable to calculate the " +
"danger in any situation.",

"The Third Spiritual Body is the Positive Mind. It is represented " +
"by Guru Amar Das - Equality." +
"A 3 is flexible and mischievous. He gets pulled down by negativity " +
"and needs a positive sense of humor." +
"The negative aspect of the number 3 is that this person will not " +
"able to see the good in situations.",

"The Fourth Body is the Neutral Mind. It is represented by Guru " +
"Ram Das - Service.",


];

function reduceNumber(num) {
var threshold = 12;
Expand Down Expand Up @@ -93,26 +69,44 @@ var KYNumbers = (function() {
return result;
}

function DescribeNumber(feature, number) {
function DescribeFeature(feature) {
}

function DescribeNumber(number) {
}

function DescribeFeatureNumber(feature, number) {
if (!feature.hasOwnProperty("name") ||
!feature.hasOwnProperty("value")) {
throw new TypeError("invalid feature parameter.");
}

if (isNaN(parseInt(feature.value)) ||
feature.value < 1 || feature.value > 5) {
feature.value < 0 || feature.value > 4) {
throw new TypeError("feature.value is invalid.");
}

if (isNaN(parseInt(number)) || number < 0 || number >= 12) {
throw new TypeError("Invalid number parameter.");
}

var numbers = data_doc.childNamed("numbers");
for (var i = 0; i < numbers.children.length; i++) {
var number_object = numbers.children[i];
if (parseInt(number_object.attr.value) === number) {
var feature_data = number_object.childNamed(feature.name);
var text = feature_data.val;
return text;
}
}
return "";
}

var module = {
GetNumbers: GetNumbers,
DescribeNumber: DescribeNumber
DescribeFeature: DescribeFeature,
DescribeNumber: DescribeNumber,
DescribeFeatureNumber: DescribeFeatureNumber
};

return module;
Expand Down
3 changes: 3 additions & 0 deletions kynumbers/numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function Main() {
for (var i = 0; i < nums.length; i++) {
var pair = nums[i];
print(pair.feature.name + " " + pair.number);
var p = KYNumbers.DescribeFeatureNumber(pair.feature,
pair.number);
print(p);
}
}

Expand Down
241 changes: 241 additions & 0 deletions kynumbers/xmldoc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
(function () {

var sax;

if (typeof module !== 'undefined' && module.exports) {
// We're being used in a Node-like environment
sax = require('sax');
}
else {
// assume it's attached to the Window object in a browser
sax = this.sax;

if (!sax) // no sax for you!
throw new Error("Expected sax to be defined. Make sure you're including sax.js before this file.");
}

/*
XmlElement is our basic building block. Everything is an XmlElement; even XmlDocument
behaves like an XmlElement by inheriting its attributes and functions.
*/

function XmlElement(tag) {
// Capture the parser object off of the XmlDocument delegate
var parser = delegates[delegates.length - 1].parser;

this.name = tag.name;
this.attr = tag.attributes || {};
this.val = "";
this.children = [];
this.firstChild = null;
this.lastChild = null;

// Assign parse information
this.line = parser.line;
this.column = parser.column;
this.position = parser.position;
this.startTagPosition = parser.startTagPosition;
}

// SaxParser handlers

XmlElement.prototype._opentag = function(tag) {

var child = new XmlElement(tag);

// add to our children array
this.children.push(child);

// update first/last pointers
if (!this.firstChild) this.firstChild = child;
this.lastChild = child;

delegates.unshift(child);
};

XmlElement.prototype._closetag = function() {
delegates.shift();
};

XmlElement.prototype._text = function(text) {
if (text) this.val += text;
};

XmlElement.prototype._cdata = function(cdata) {
if (cdata) this.val += cdata;
};

XmlElement.prototype._error = function(err) {
throw err;
};

// Useful functions

XmlElement.prototype.eachChild = function(iterator, context) {
for (var i=0, l=this.children.length; i<l; i++)
if (iterator.call(context, this.children[i], i, this.children) === false) return;
};

XmlElement.prototype.childNamed = function(name) {
for (var i=0, l=this.children.length; i<l; i++) {
var child = this.children[i];
if (child.name === name) return child;
}
return undefined;
};

XmlElement.prototype.childrenNamed = function(name) {
var matches = [];

for (var i=0, l=this.children.length; i<l; i++)
if (this.children[i].name === name)
matches.push(this.children[i]);

return matches;
};

XmlElement.prototype.childWithAttribute = function(name,value) {
for (var i=0, l=this.children.length; i<l; i++) {
var child = this.children[i];
if ( (value && child.attr[name] === value) || (!value && child.attr[name]) )
return child;
}
return undefined;
};

XmlElement.prototype.descendantWithPath = function(path) {
var descendant = this;
var components = path.split('.');

for (var i=0, l=components.length; i<l; i++)
if (descendant)
descendant = descendant.childNamed(components[i]);
else
return undefined;

return descendant;
};

XmlElement.prototype.valueWithPath = function(path) {
var components = path.split('@');
var descendant = this.descendantWithPath(components[0]);
if (descendant)
return components.length > 1 ? descendant.attr[components[1]] : descendant.val;
else
return undefined;
};

// String formatting (for debugging)

XmlElement.prototype.toString = function(options) {
return this.toStringWithIndent("", options);
};

XmlElement.prototype.toStringWithIndent = function(indent, options) {
var s = indent + "<" + this.name;
var linebreak = options && options.compressed ? "" : "\n";

for (var name in this.attr)
if (Object.prototype.hasOwnProperty.call(this.attr, name))
s += " " + name + '="' + this.attr[name] + '"';

var finalVal = this.val.trim().replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, '&amp;');

if (options && options.trimmed && finalVal.length > 25)
finalVal = finalVal.substring(0,25).trim() + "…";

if (this.children.length) {
s += ">" + linebreak;

var childIndent = indent + (options && options.compressed ? "" : " ");

if (finalVal.length)
s += childIndent + finalVal + linebreak;

for (var i=0, l=this.children.length; i<l; i++)
s += this.children[i].toStringWithIndent(childIndent, options) + linebreak;

s += indent + "</" + this.name + ">";
}
else if (finalVal.length) {
s += ">" + finalVal + "</" + this.name +">";
}
else s += "/>";

return s;
};

/*
XmlDocument is the class we expose to the user; it uses the sax parser to create a hierarchy
of XmlElements.
*/

function XmlDocument(xml) {
xml && (xml = xml.toString().trim());

if (!xml)
throw new Error("No XML to parse!");

// Expose the parser to the other delegates while the parser is running
this.parser = sax.parser(true); // strict
addParserEvents(this.parser);

// We'll use the file-scoped "delegates" var to remember what elements we're currently
// parsing; they will push and pop off the stack as we get deeper into the XML hierarchy.
// It's safe to use a global because JS is single-threaded.
delegates = [this];

this.parser.write(xml);

// Remove the parser as it is no longer needed and should not be exposed to clients
delete this.parser;
}

// make XmlDocument inherit XmlElement's methods
extend(XmlDocument.prototype, XmlElement.prototype);

XmlDocument.prototype._opentag = function(tag) {
if (typeof this.children === 'undefined')
// the first tag we encounter should be the root - we'll "become" the root XmlElement
XmlElement.call(this,tag);
else
// all other tags will be the root element's children
XmlElement.prototype._opentag.apply(this,arguments);
};

// file-scoped global stack of delegates
var delegates = null;

/*
Helper functions
*/

function addParserEvents(parser) {
parser.onopentag = parser_opentag;
parser.onclosetag = parser_closetag;
parser.ontext = parser_text;
parser.oncdata = parser_cdata;
parser.onerror = parser_error;
}

// create these closures and cache them by keeping them file-scoped
function parser_opentag() { delegates[0]._opentag.apply(delegates[0],arguments) }
function parser_closetag() { delegates[0]._closetag.apply(delegates[0],arguments) }
function parser_text() { delegates[0]._text.apply(delegates[0],arguments) }
function parser_cdata() { delegates[0]._cdata.apply(delegates[0],arguments) }
function parser_error() { delegates[0]._error.apply(delegates[0],arguments) }

// a relatively standard extend method
function extend(destination, source) {
for (var prop in source)
if (source.hasOwnProperty(prop))
destination[prop] = source[prop];
}

// Are we being used in a Node-like environment?
if (typeof module !== 'undefined' && module.exports)
module.exports.XmlDocument = XmlDocument;
else
this.XmlDocument = XmlDocument;

})();

0 comments on commit 0496b40

Please sign in to comment.