Created
September 28, 2011 09:09
-
-
Save lambdalisue/1247426 to your computer and use it in GitHub Desktop.
CoffeeScript Snnipets
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
### | |
Detect browser name, version and OS | |
@ref: http://www.quirksmode.org/js/detect.html | |
### | |
class Detector | |
constructor: -> | |
@browser = @searchString(Detector.dataBrowser) or "An unknown browser" | |
@version = @searchVersion(navigator.userAgent) or @searchVersion(navigator.appVersion) or "An unknown browser" | |
@OS = @searchString(Detector.dataOS) or "An unknown OS" | |
searchString: (data) -> | |
for row in data | |
@versionSearchString = row.versionSearch or row.identify | |
if row.string? | |
if row.string.indexOf(row.subString) isnt -1 | |
return row.identify | |
else if row.prop | |
return row.identify | |
searchVersion: (dataString) -> | |
index = dataString.indexOf @versionSearchString | |
if index is -1 then return | |
return parseFloat dataString.substring(index+@versionSearchString.length+1) | |
@dataBrowser: [ | |
{string: navigator.userAgent, subString: 'Chrome', identify: 'Chrome'} | |
{string: navigator.userAgent, subString: 'OmniWeb', versionSearch: 'OmniWeb/', identify: 'OmniWeb'} | |
{string: navigator.vendor, subString: 'Apple', identify: 'Safari', versionSearch: 'Version'} | |
{prop: window.opera, identify: 'Opera', versionSearch: 'Version'} | |
{string: navigator.vendor, subString: 'iCab', identify: 'iCab'} | |
{string: navigator.vendor, subString: 'KDE', identify: 'Konqueror'} | |
{string: navigator.userAgent, subString: 'Firefox', identify: 'Firefox'} | |
{string: navigator.vendor, subString: 'Camino', identify: 'Camino'} | |
{string: navigator.userAgent, subString: 'Netscape', identify: 'Netscape'} | |
{string: navigator.userAgent, subString: 'MSIE', identify: 'Explorer', versionSearch: 'MSIE'} | |
{string: navigator.userAgent, subString: 'Gecko', identify: 'Mozilla', versionSearch: 'rv'} | |
{string: navigator.userAgent, subString: 'Mozilla', identify: 'Netscape', versionSearch: 'Mozilla'} | |
] | |
@dataOS: [ | |
{string: navigator.platform, subString: 'Win', identify: 'Windows'} | |
{string: navigator.platform, subString: 'Mac', identify: 'Mac'} | |
{string: navigator.userAgent, subString: 'iPhone', identify: 'iPhone/iPad'} | |
{string: navigator.platform, subString: 'Linux', identify: 'Linux'} | |
] | |
exports?.Detector = Detector |
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
### | |
partial | |
CoffeeScript partial class utils. it is useful when you create a large class | |
and want split the class to several files. | |
Author: Alisue (lambdalisue@hashnote.net) | |
License: MIT License | |
Copyright 2011 hashnote.net, Alisue allright reserved | |
Example: | |
// person.core.coffee | |
class Person | |
constructor: (@name) -> @ | |
say: -> "My name is #{@name}" | |
// person.hobby.coffee | |
Person = partial Person, | |
chess: -> 'Yes I like' | |
// person.food.coffee | |
Person = partial Person, | |
apple: -> 'No i don't like it' | |
### | |
partial = (cls, prototypes) -> | |
for name, callback of prototypes | |
cls.prototype[name] = callback | |
return cls | |
exports?.partial = partial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Surely you do not need to return cls in the partial function? cls is a reference to the object (I think) and as such is changing it's properties directly. You should be able to do this if you think about it:
Untested, so I'm probably wrong.