Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit c23133da04ac5760e45e49b685d7af10568041a9 1 parent ee54bc3
Nico Wiedemann authored
Showing with 250 additions and 3 deletions.
  1. +165 −0 .gitignore
  2. +23 −3 README.md
  3. +53 −0 switcher.js
  4. +9 −0 switcher.sublime-project
165 .gitignore
View
@@ -0,0 +1,165 @@
+*.sublime-workspace
+
+#################
+## Eclipse
+#################
+
+*.pydevproject
+.project
+.metadata
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.classpath
+.settings/
+.loadpath
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# PDT-specific
+.buildpath
+
+
+#################
+## Visual Studio
+#################
+
+## Ignore Visual Studio temporary files, build results, and
+## files generated by popular Visual Studio add-ons.
+
+# User-specific files
+*.suo
+*.user
+*.sln.docstates
+
+# Build results
+[Dd]ebug/
+[Rr]elease/
+*_i.c
+*_p.c
+*.ilk
+*.meta
+*.obj
+*.pch
+*.pdb
+*.pgc
+*.pgd
+*.rsp
+*.sbr
+*.tlb
+*.tli
+*.tlh
+*.tmp
+*.vspscc
+.builds
+*.dotCover
+
+## TODO: If you have NuGet Package Restore enabled, uncomment this
+#packages/
+
+# Visual C++ cache files
+ipch/
+*.aps
+*.ncb
+*.opensdf
+*.sdf
+
+# Visual Studio profiler
+*.psess
+*.vsp
+
+# ReSharper is a .NET coding add-in
+_ReSharper*
+
+# Installshield output folder
+[Ee]xpress
+
+# DocProject is a documentation generator add-in
+DocProject/buildhelp/
+DocProject/Help/*.HxT
+DocProject/Help/*.HxC
+DocProject/Help/*.hhc
+DocProject/Help/*.hhk
+DocProject/Help/*.hhp
+DocProject/Help/Html2
+DocProject/Help/html
+
+# Click-Once directory
+publish
+
+# Others
+[Bb]in
+[Oo]bj
+sql
+TestResults
+*.Cache
+ClientBin
+stylecop.*
+~$*
+*.dbmdl
+Generated_Code #added for RIA/Silverlight projects
+
+# Backup & report files from converting an old project file to a newer
+# Visual Studio version. Backup files are not needed, because we have git ;-)
+_UpgradeReport_Files/
+Backup*/
+UpgradeLog*.XML
+
+
+
+############
+## Windows
+############
+
+# Windows image file caches
+Thumbs.db
+
+# Folder config file
+Desktop.ini
+
+
+#############
+## Python
+#############
+
+*.py[co]
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+
+#Translations
+*.mo
+
+#Mr Developer
+.mr.developer.cfg
+
+# Mac crap
+.DS_Store
26 README.md
View
@@ -1,4 +1,24 @@
-switcher
-========
+switcher provides a similar syntax to a switch statement in JavaScript but with RegExp as cases.
-switcher provides a similar syntax to a switch statement in JavaScript but with RegExp as cases
+# How to use
+I tried to mimic the syntax of a switch statement as good as possible. The following example shows how to match against the UserAgent string to detect iOS devices.
+
+```javascript
+switcher(navigator.userAgent, {
+ '/i(OS|Pad|Phone|Pod)/': function (scope, expression) {
+ console.log('iOS device! '+scope+' did match '+expression);
+ },
+ '': function () {
+ console.log('any device');
+ }
+});
+```
+
+Note that the Regular Expressions are written as Strings as JavaScript doesn't allow RegExp objects as keys. The default case is represented by an empty string as this one matches everything. You should always put the default case at last as with ``breakMode`` set to ``true`` no cases after the default one could be reached.
+
+# Options
+``breakMode``
+When set to ``true`` (which is default) switcher will stop testing after the first match. When set to ``false`` switcher will continue testing (but note that changes that may have been made by previous matches can be overwritten ‒ *especially if you have a default case at the end*).
+
+# License
+[MIT license](http://opensource.org/licenses/MIT)
53 switcher.js
View
@@ -0,0 +1,53 @@
+/*!
+ * switcher 1.0 (c) 2012 Nico Wiedemann - MIT license
+ */
+!function (definition) {
+ if (typeof define==="function" && define.amd)
+ define(definition);
+ else
+ self.switcher = definition();
+}(function () {
+ /*switcher(navigator.userAgent, {
+ '': function (scope) {
+ // default case (should be last with breakMode set to true!)
+ },
+ '/i(OS|Pad|Phone|Pod)/': function () {
+
+ }
+ })*/
+ 'use strict';
+ /**
+ * function to provide a similar syntax to a switch statement but with regex as cases
+ * @param {String} scope string on which the regexes should be tested against
+ * @param {Object} tests object containing regexes as keys and callback functions as values which will be called with the scope as first parameter if the regex match.
+ * @param {Object} options object containing options like breakMode (see https://github.com/EarMaster/switcher for full list of options)
+ * @return {Boolean} returns true if one or more tests matched
+ */
+ var switcher = function (scope, tests, options) {
+ var defaultOptions = {
+ breakMode: true // if set to true switcher will stop after the first match
+ };
+ if (!options)
+ options = defaultOptions;
+ else
+ for (var option in defaultOptions)
+ if (!options.hasOwnProperty(option))
+ options[option] = defaultOptions[option];
+ var matched = false;
+ for (var test in tests) {
+ test = {
+ fullExpression: test,
+ expression: test.substr(0,1)=='/'?test.substr(1, test.lastIndexOf('/')-1):test,
+ flags: test.substr(0,1)=='/'?test.substr(test.lastIndexOf('/')+1):''
+ };
+ if (new RegExp(test.expression, test.flags).test(scope)) {
+ matched = true;
+ tests[test.fullExpression](scope, test.fullExpression);
+ if (options.breakMode)
+ break;
+ }
+ }
+ return matched;
+ };
+ return switcher;
+});
9 switcher.sublime-project
View
@@ -0,0 +1,9 @@
+{
+ "folders":
+ [
+ {
+ "path": "/",
+ "file_exclude_patterns": [".gitignore","*.sublime-*"]
+ }
+ ]
+}
Please sign in to comment.
Something went wrong with that request. Please try again.