Click here to Skip to main content
15,896,063 members
Articles / Web Development / Node.js

Node.Js And Stuff

Rate me:
Please Sign up or sign in to vote.
4.97/5 (55 votes)
11 Feb 2013CPOL23 min read 361.5K   2.3K   172  
Small demo app using Node.Js/Socket.IO/MongoDB/D3.Js and jQuery.
vendors = moz webkit o ms official

// stringify the given arg

-string(arg)
  type(arg) + ' ' + arg

// require a color

require-color(color)
  unless color is a 'color' 
    error('RGB or HSL value expected, got a ' + -string(color))

// require a unit

require-unit(n)
  unless n is a 'unit'
    error('unit expected, got a ' + -string(n))

// require a string

require-string(str)
  unless str is a 'string' or str is a 'ident'
    error('string expected, got a ' + -string(str))

// apply js Math function

math(n, fn) 
  require-unit(n)
  require-string(fn)
  -math(n, fn)

// adjust the given color's property by amount

adjust(color, prop, amount)
  require-color(color)
  require-string(prop)
  require-unit(amount)
  -adjust(color, prop, amount)

// Math functions

abs(n) { math(n, 'abs') }
ceil(n) { math(n, 'ceil') }
floor(n) { math(n, 'floor') }
round(n) { math(n, 'round') }
sin(n) { math(n, 'sin') }
cos(n) { math(n, 'cos') }
min(a, b) { a < b ? a : b }
max(a, b) { a > b ? a : b }
PI = -math-prop('PI')

// return the sum of the given numbers

sum(nums)
  sum = 0
  sum += n for n in nums

// return the average of the given numbers

avg(nums)
  sum(nums) / length(nums)

// return a unitless number, or pass through

remove-unit(n)
  if typeof(n) is "unit"
    unit(n, "")
  else
    n

// convert a percent to a decimal, or pass through

percent-to-decimal(n)
  if unit(n) is "%"
    remove-unit(n) / 100
  else
    n

// color components

alpha(color) { component(hsl(color), 'alpha') }
hue(color) { component(hsl(color), 'hue') }
saturation(color) { component(hsl(color), 'saturation') }
lightness(color) { component(hsl(color), 'lightness') }

// check if n is an odd number

odd(n)
  1 == n % 2

// check if n is an even number

even(n)
  0 == n % 2

// check if color is light

light(color)
  lightness(color) >= 50%

// check if color is dark

dark(color)
  lightness(color) < 50%

// desaturate color by amount

desaturate(color, amount)
  adjust(color, 'saturation', - amount)

// saturate color by amount

saturate(color, amount)
  adjust(color, 'saturation', amount)

// darken by the given amount

darken(color, amount)
  adjust(color, 'lightness', - amount)

// lighten by the given amount

lighten(color, amount)
  adjust(color, 'lightness', amount)

// decerase opacity by amount

fade-out(color, amount)
  color - rgba(black, percent-to-decimal(amount))
  
// increase opacity by amount

fade-in(color, amount)
  color + rgba(black, percent-to-decimal(amount))

// spin hue by a given amount

spin(color, amount)
  color + unit(amount, deg)

// mix two colors by a given amount

mix(color1, color2, weight = 50%)
  unless weight in 0..100
    error("Weight must be between 0% and 100%")

  if length(color1) == 2
    weight = color1[0]
    color1 = color1[1]

  else if length(color2) == 2
    weight = 100 - color2[0]
    color2 = color2[1]

  require-color(color1)
  require-color(color2)

  p = unit(weight / 100, '')
  w = p * 2 - 1

  a = alpha(color1) - alpha(color2)

  w1 = (((w * a == -1) ? w : (w + a) / (1 + w * a)) + 1) / 2
  w2 = 1 - w1

  channels = (red(color1) red(color2)) (green(color1) green(color2)) (blue(color1) blue(color2))
  rgb = ()

  for pair in channels
    push(rgb, floor(pair[0] * w1 + pair[1] * w2))

  a1 = alpha(color1) * p
  a2 = alpha(color1) * (1 - p)
  alpha = a1 + a2

  rgba(rgb[0], rgb[1], rgb[2], alpha)

// invert colors, leave alpha intact

invert(color)
  r = 255 - red(color)
  g = 255 - green(color)
  b = 255 - blue(color)
  rgba(r,g,b,alpha(color))

// return the last value in the given expr

last(expr)
  expr[length(expr) - 1]

// return keys in the given pairs

keys(pairs)
  ret = ()
  for pair in pairs
    push(ret, pair[0]);
  ret

// return values in the given pairs

values(pairs)
  ret = ()
  for pair in pairs
    push(ret, pair[1]);
  ret

// join values with the given delimiter

join(delim, vals...)
  buf = ''
  vals = vals[0] if length(vals) == 1
  for val, i in vals
    buf += i ? delim + val : val

// add a css rule to the containing block

// - This definition allows add-property to be used as a mixin
// - It has the same effect as interpolation but allows users 
//   to opt for a functional style

add-property-function = add-property
add-property(name, expr)
  if mixin
    {name} expr
  else
    add-property-function(name, expr)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions