Str.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Generated by LiveScript 1.6.0
  2. var split, join, lines, unlines, words, unwords, chars, unchars, reverse, repeat, capitalize, camelize, dasherize;
  3. split = curry$(function(sep, str){
  4. return str.split(sep);
  5. });
  6. join = curry$(function(sep, xs){
  7. return xs.join(sep);
  8. });
  9. lines = function(str){
  10. if (!str.length) {
  11. return [];
  12. }
  13. return str.split('\n');
  14. };
  15. unlines = function(it){
  16. return it.join('\n');
  17. };
  18. words = function(str){
  19. if (!str.length) {
  20. return [];
  21. }
  22. return str.split(/[ ]+/);
  23. };
  24. unwords = function(it){
  25. return it.join(' ');
  26. };
  27. chars = function(it){
  28. return it.split('');
  29. };
  30. unchars = function(it){
  31. return it.join('');
  32. };
  33. reverse = function(str){
  34. return str.split('').reverse().join('');
  35. };
  36. repeat = curry$(function(n, str){
  37. var result, i$;
  38. result = '';
  39. for (i$ = 0; i$ < n; ++i$) {
  40. result += str;
  41. }
  42. return result;
  43. });
  44. capitalize = function(str){
  45. return str.charAt(0).toUpperCase() + str.slice(1);
  46. };
  47. camelize = function(it){
  48. return it.replace(/[-_]+(.)?/g, function(arg$, c){
  49. return (c != null ? c : '').toUpperCase();
  50. });
  51. };
  52. dasherize = function(str){
  53. return str.replace(/([^-A-Z])([A-Z]+)/g, function(arg$, lower, upper){
  54. return lower + "-" + (upper.length > 1
  55. ? upper
  56. : upper.toLowerCase());
  57. }).replace(/^([A-Z]+)/, function(arg$, upper){
  58. if (upper.length > 1) {
  59. return upper + "-";
  60. } else {
  61. return upper.toLowerCase();
  62. }
  63. });
  64. };
  65. module.exports = {
  66. split: split,
  67. join: join,
  68. lines: lines,
  69. unlines: unlines,
  70. words: words,
  71. unwords: unwords,
  72. chars: chars,
  73. unchars: unchars,
  74. reverse: reverse,
  75. repeat: repeat,
  76. capitalize: capitalize,
  77. camelize: camelize,
  78. dasherize: dasherize
  79. };
  80. function curry$(f, bound){
  81. var context,
  82. _curry = function(args) {
  83. return f.length > 1 ? function(){
  84. var params = args ? args.concat() : [];
  85. context = bound ? context || this : this;
  86. return params.push.apply(params, arguments) <
  87. f.length && arguments.length ?
  88. _curry.call(context, params) : f.apply(context, params);
  89. } : f;
  90. };
  91. return _curry();
  92. }