Useful javascript

264 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
264
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Useful javascript

  1. 1. Some Javascript useful perhaps
  2. 2. 他山之石,可以攻玉Stones from other hills may serveto polish the jade of this one.advice from others may help oneovercome ones shortcomings.
  3. 3. Ruby A Programmer’s Best FriendThis time we use Ruby to polish our Jade.
  4. 4. Integer.times (10).times(function10.times do |i| (i){print i*10, " " print( i*10 + “ ”);end });
  5. 5. Number.step1.step(10, 2) { |i| print i, " " }Math::E.step(Math::PI, 0.2) { |f| print f, " " }(1).step(function(i){ print(i); }, 10, 2);Math.E.step(function(f){ print(f); },
  6. 6. Number Object modulo abs ceil floorround step
  7. 7. String.capitalise"hello".capitalize #=> "Hello""HELLO".capitalize#=> "Hello""123ABC".capitalize#=>"123abc""hello".capitalize() > "Hello""HELLO".capitalize() > "Hello" "123ABC".capitalize() > "123abc"
  8. 8. String.each_char"hello".each_char {|c| print c, } #=> "h e l l o"“hello”.each_char (function(c) {return c+” ”}) > “he l l o ”
  9. 9. String.insert"abcd".insert(-3, X) #=> abXcd"abcd".insert(-1, X) #=> abcdX"abcd".insert(1, X) #=> aXbcd"abcd".insert(-3, X) > abXcd"abcd".insert(-1, X) > abcdX"abcd".insert(1, X) > aXbcd
  10. 10. String.reverse"Hello".reverse #=> "olleh""Hello".reverse() > "olleh"
  11. 11. String Object casecmp inserteach_char capitalisestrip swapcase start_with end_with reverse
  12. 12. Array.each[1,2,3].each {|i| print i+5 } #=> 6 7 8[1,2,3].each(function(i){ print( i+5) }) > 6 7 8[1,2,3].reverse_each(function(i){ print( i+5) }) >8 7 6[{a:1,b:2},{a:2,b:3},{a:3,b:4}].each(function(item){ print(item.a+item.b)}) > 3 5 7
  13. 13. Array.mapa = [ "a", "b", "c", "d" ]a.map {|x|x+"!" } #=> ["a!", "b!", "c!", "d!"]a #=> ["a", "b", "c", "d"]var a = [ "a", "b", "c", "d" ];a.map(function(i,item){return x+”!”}) > ["a!","b!", "c!", "d!"]a > [ "a","b", "c", "d" ]
  14. 14. Array.remove_if (Array.reject)[ "a", "b", "c" ].reject {|x| x>="b" } #=> ["a"][ "a", "b", "c" ].reject( function(i,x) {return x >="b" })
  15. 15. a = [ 4, 5, 6 ] Array.zipb = [ 7, 8, 9 ][1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3,6, 9]][1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]a.zip([1,2],[8]) #=> [[4, 1, 8], [5, 2, nil],var a = [ 4, 5, 6 ];[6, nil, nil]]var b = [ 7, 8, 9 ];[1,2,3].zip(a, b) > [[1, 4, 7], [2, 5, 8], [3, 6,9]][1,2].zip(a,b) > [[1, 4, 7], [2, 5, 8]]a.zip([1,2],[8]) > [[4, 1, 8], [5, 2, null], [6,null, null]]
  16. 16. Array.transposea = [[1,2], [3,4], [5,6]]a.transpose #=> [[1, 3, 5], [2, 4,6]]var a = [[1,2], [3,4], [5,6]];a.transpose() > [[1, 3, 5], [2, 4,6]]
  17. 17. Array.rotatea = [ "a", "b", "c", "d" ]a.rotate #=> ["b", "c", "d", "a"]a #=> ["a", "b", "c", "d"]a.rotate(2) #=> ["c", "d", "a", "b"]a.rotate(-3) #=> ["b", "c", "d", "a"]var a = [ "a", "b", "c", "d" ];a.rotate() > ["b", "c", "d", "a"]a > ["a", "b", "c", "d"]a.rotate(2) > ["c", "d", "a", "b"]a.rotate(-3) > ["b", "c", "d", "a"]
  18. 18. Array.flattens = [ 1, 2, 3 ] #=> [1, 2, 3]t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7,8]], 9, 10]a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8,9, 10]a = [ 1, 2, [3, [4, 5] ] ]a.flatten(1) #=> [1, 2, 3, [4, 5]]
  19. 19. Array.sample[1,2,3,4,5,6,7,8,9,10].sample() #=> 7 (justrandomly selected)[1,2,3,4,5,6,7,8,9,10].sample(3) #=> 3, 9, 2
  20. 20. Array.shufflea = [ 1, 2, 3 ] #=> [1, 2, 3]a.shuffle #=> [2, 3, 1]var a = [ 1, 2, 3 ]; > [1, 2, 3]a.shuffle() > [2, 3, 1]
  21. 21. Array Object is_empty push_all size reverse_eachintersect clear each deduct map uniq unionsample at shuffle values_attranspose contains last transpose keep_if compact select remove insert remove_if reject zip index rotate equals take remove_at fetchtake_while flatten count
  22. 22. Examples(1..10).map{|i| ("a".."z").to_a[rand 26] }.join new Array(10).map(function(i,item){ return"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];}).join("");(10).times(function(i){ return"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];}).join("");
  23. 23. Examples(1..10).map{|i| ("a".."z").to_a[rand 26] }.join new Array(10).map(function(i,item){ return"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];}).join("");(10).times(function(i){ return"abcdefghijklmnopqrstuvwxyz"[(26*Math.random()).floor()];}).join("");
  24. 24. How I simply try these snippets nodejs irb
  25. 25. Thanks to
  26. 26. What if we can write and test like thisrequire ../src/com/ciphor/ruby/Array.jsdescribe com.ciphor.ruby.Array, -> testArray = null beforeEach -> testArray = [1, 2, 3, 4, 5] Test for BDD - Behaviour Driven afterEach -> testArray = null Developement it adds all elements in the given array into the self, -> testArray.push_all [6, 7, 8] expect(testArray.length).toEqual(8) expect(testArray).toContain(8)
  27. 27. +...to be continued
  28. 28. NotThe End

×