1
0

junk-drawer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //jshint eqnull:true
  2. define(function () {
  3. "use strict";
  4. function inherits (child, parent) {
  5. child.prototype = Object.create(parent.prototype, {
  6. constructor: {
  7. value: child,
  8. enumerable: false, configurable: true, writable: true
  9. }
  10. });
  11. return child;
  12. }
  13. function installMethodOfJsObject (obj, name, fn) {
  14. Object.defineProperty(obj, name, {
  15. value: fn,
  16. enumerable: false, configurable: true, writable: true
  17. });
  18. }
  19. function noop () {
  20. }
  21. function declareJsMethod (obj, name) {
  22. if (obj[name] == null) installMethodOfJsObject(obj, name, noop);
  23. }
  24. var table = {
  25. ':': '_',
  26. '&': '_and',
  27. '|': '_or',
  28. '+': '_plus',
  29. '-': '_minus',
  30. '*': '_star',
  31. '/': '_slash',
  32. '\\': '_backslash',
  33. '~': '_tild',
  34. '%': '_percent',
  35. '>': '_gt',
  36. '<': '_lt',
  37. '=': '_eq',
  38. ',': '_comma',
  39. '@': '_at'
  40. };
  41. /* Convert a Smalltalk selector into a JS selector */
  42. function st2js (string) {
  43. return '_' + string
  44. .replace(/[:&|+\-*/\\~%><=,@]/g, function (ch) {
  45. return table[ch];
  46. });
  47. };
  48. function js2st (selector) {
  49. if (selector.match(/^__/)) {
  50. return binaryJsToSt(selector);
  51. } else {
  52. return keywordJsToSt(selector);
  53. }
  54. }
  55. function keywordJsToSt (selector) {
  56. return selector.replace(/^_/, '').replace(/_/g, ':');
  57. }
  58. function binaryJsToSt (selector) {
  59. return selector
  60. .replace(/^_/, '')
  61. .replace(/_and/g, '&')
  62. .replace(/_or/g, '|')
  63. .replace(/_plus/g, '+')
  64. .replace(/_minus/g, '-')
  65. .replace(/_star/g, '*')
  66. .replace(/_slash/g, '/')
  67. .replace(/_backslash/g, '\\')
  68. .replace(/_tild/g, '~')
  69. .replace(/_percent/g, '%')
  70. .replace(/_gt/g, '>')
  71. .replace(/_lt/g, '<')
  72. .replace(/_eq/g, '=')
  73. .replace(/_comma/g, ',')
  74. .replace(/_at/g, '@');
  75. }
  76. function addElement (array, el) {
  77. if (typeof el === 'undefined') {
  78. return;
  79. }
  80. if (array.indexOf(el) === -1) {
  81. array.push(el);
  82. }
  83. }
  84. function removeElement (array, el) {
  85. var i = array.indexOf(el);
  86. if (i !== -1) {
  87. array.splice(i, 1);
  88. }
  89. }
  90. function extend (target, source) {
  91. Object.keys(source).forEach(function (key) {
  92. target[key] = source[key];
  93. });
  94. return target;
  95. }
  96. function extendWithMethods (target, source) {
  97. Object.keys(source).forEach(function (key) {
  98. installMethodOfJsObject(target, key, source[key]);
  99. });
  100. return target;
  101. }
  102. function deleteKeysFrom (keys, obj) {
  103. keys.forEach(function (each) {
  104. delete obj[each];
  105. });
  106. }
  107. return {
  108. deleteKeysFrom: deleteKeysFrom,
  109. extendWithMethods: extendWithMethods,
  110. extend: extend,
  111. removeElement: removeElement,
  112. addElement: addElement,
  113. js2st: js2st,
  114. st2js: st2js,
  115. declareJsMethod: declareJsMethod,
  116. installMethodOfJsObject: installMethodOfJsObject,
  117. inherits: inherits
  118. }
  119. });