Last active
October 12, 2015 04:58
-
-
Save felipegtx/3974654 to your computer and use it in GitHub Desktop.
Base lib for JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https: //gist.github.com/felipegtx/3974654 | |
(function (window) { | |
window.Base = (function () { | |
var publicMembers = { | |
CreatePropertyBag: function (owner, defaults) { | |
/// <summary> | |
/// Creates a properties bag for keeping data across the current page state | |
/// </summary> | |
var owner = owner ? owner : this; | |
var props = defaults ? defaults : {}; | |
return { | |
SetAndReturnOwner: function (name, value) { | |
this.Set(name, value); | |
return owner; | |
}, | |
Set: function (name, value) { | |
props[name] = value; | |
}, | |
Get: function (name) { | |
return props[name]; | |
}, | |
Format: function (data) { | |
return publicMembers.Format(data, props); | |
} | |
}; | |
}, | |
Format: function (strInput, objData) { | |
/// <summary> | |
/// Parses a given string based on a given data. | |
/// Eg.: | |
/// Given the following input: | |
/// strInput = "Hello {World}", objData = {World:"Earth"} | |
/// The result would be the following string "Hello Earth" | |
/// | |
/// Given the following input: | |
/// strInput = "Hello {0}", objData = ["Goodbye"] | |
/// The result would be the following string "Hello Goodbye" | |
/// </summary> | |
objData = typeof objData === 'object' ? objData : Array.prototype.slice.call(arguments, 1); | |
if (strInput === null) { return; } | |
return strInput.replace(/\{\{|\}\}|\{(\w+(\.+|\w)*)\}/g, function (m, n) { | |
if (m == "{{") { return "{"; } | |
if (m == "}}") { return "}"; } | |
var value = (n.split(".").length > 1) ? GetValueFor(n, objData) : objData[n]; | |
return ParseDate(value); | |
}); | |
} | |
}; | |
function GetValueFor(strField, objData) { | |
/// <summary> | |
/// Gathers value from a given object data | |
/// Eg.: | |
/// Given the following input: | |
/// strField = "Data.Key.Value" / objData = { Data : { Key : { Value : "My mapped data" } } } | |
/// The result from this function is a string with the following value: "My mapped data" | |
/// </summary> | |
var dataArray = strField.split("."); | |
var value = ""; | |
for (var item in objData) { | |
if (item == dataArray[0]) { | |
if (dataArray.length > 1) { | |
return GetValueFor(strField.substr(strField.indexOf(".") + 1), objData[item]); | |
} | |
else { | |
return objData[item]; | |
} | |
} | |
} | |
} | |
function ParseDate(strValue) { | |
/// <summary> | |
/// Parses a given date value | |
/// </summary> | |
var finalValue = strValue; | |
if (/\/Date\(\d+\)\//.test(strValue)) { | |
finalValue = new Date(parseInt(/-?\d+/.exec(strValue)[0])).toString("dd/MM/yyyy"); | |
} | |
return finalValue; | |
} | |
return publicMembers; | |
})(); | |
})(window); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Creates a new property bag | |
var a = (function Do(){ | |
var propertyBag = Base.CreatePropertyBag(this /*referência para o owner da instância*/, {name: "Derp"} /*default values*/); | |
var publicMembers = { | |
SetName:function(value){propertyBag.SetAndReturnOwner("name", value);}, | |
SetAge:function(value){propertyBag.SetAndReturnOwner("age", value);}, | |
PrintData:function(){propertyBag.Format("Name: {name} - Age: {age}");} | |
}; | |
return publicMembers; | |
})() | |
a.SetName("test").SetAge(2).PrintData(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alert(Base.CreatePropertyBag().Set("name", "derp").Format("Hi {name}!")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment