Skip to content

Commit

Permalink
Add parsing of non-json strings with options
Browse files Browse the repository at this point in the history
  • Loading branch information
vodkabears committed Jul 28, 2014
1 parent d76f925 commit 7704731
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/jquery.remodal.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,40 @@
$("html, body").removeClass(pluginName + "_lock");
};

/**
* Parse string with options
* @param str
* @returns {Object}
*/
var parseOptions = function (str) {
var obj = {}, clearedStr, arr;

// remove spaces before and after delimiters
clearedStr = str.replace(/\s*:\s*/g, ":").replace(/\s*,\s*/g, ",");

// parse string
arr = clearedStr.split(",");
var i, len, val;
for (i = 0, len = arr.length; i < len; i++) {
arr[i] = arr[i].split(":");
val = arr[i][1];

// convert string value if it is like a boolean
if (typeof val === "string" || val instanceof String) {
val = val === "true" || (val === "false" ? false : val);
}

// convert string value if it is like a number
if (typeof val === "string" || val instanceof String) {
val = !isNaN(val) ? +val : val;
}

obj[arr[i][0]] = val;
}

return obj;
};

/**
* Remodal constructor
*/
Expand Down Expand Up @@ -286,6 +320,8 @@

if (!options) {
options = {};
} else if (typeof options == "string" || options instanceof String) {
options = parseOptions(options);
}

$container[pluginName](options);
Expand Down
20 changes: 20 additions & 0 deletions test/remodal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vide Test Suite</title>
<!-- Load local jQuery. This can be overridden with a ?jquery=___ param. -->
<script src="../libs/jquery-loader.js"></script>
<!-- Load local QUnit. -->
<link rel="stylesheet" href="../libs/qunit/qunit.css" media="screen">
<script src="../libs/qunit/qunit.js"></script>
<!-- Load local lib and tests. -->
<script src="../src/jquery.remodal.js"></script>
<script src="remodal_test.js"></script>
<script>window._$ = jQuery.noConflict(true);</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
</body>
</html>
23 changes: 23 additions & 0 deletions test/remodal_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(function ($) {
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
Test methods:
module(name, {[setup][ ,teardown]})
test(name, callback)
expect(numberOfAssertions)
stop(increment)
start(decrement)
Test assertions:
ok(value, [message])
equal(actual, expected, [message])
notEqual(actual, expected, [message])
deepEqual(actual, expected, [message])
notDeepEqual(actual, expected, [message])
strictEqual(actual, expected, [message])
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/

}(jQuery));

0 comments on commit 7704731

Please sign in to comment.