junk-drawer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. /* Convert a Smalltalk selector into a JS selector */
  25. function st2js (string) {
  26. return '_' + string
  27. .replace(/:/g, '_')
  28. .replace(/[\&]/g, '_and')
  29. .replace(/[\|]/g, '_or')
  30. .replace(/[+]/g, '_plus')
  31. .replace(/-/g, '_minus')
  32. .replace(/[*]/g, '_star')
  33. .replace(/[\/]/g, '_slash')
  34. .replace(/[\\]/g, '_backslash')
  35. .replace(/[\~]/g, '_tild')
  36. .replace(/%/g, '_percent')
  37. .replace(/>/g, '_gt')
  38. .replace(/</g, '_lt')
  39. .replace(/=/g, '_eq')
  40. .replace(/,/g, '_comma')
  41. .replace(/[@]/g, '_at');
  42. };
  43. function js2st (selector) {
  44. if (selector.match(/^__/)) {
  45. return binaryJsToSt(selector);
  46. } else {
  47. return keywordJsToSt(selector);
  48. }
  49. }
  50. function keywordJsToSt (selector) {
  51. return selector.replace(/^_/, '').replace(/_/g, ':');
  52. }
  53. function binaryJsToSt (selector) {
  54. return selector
  55. .replace(/^_/, '')
  56. .replace(/_and/g, '&')
  57. .replace(/_or/g, '|')
  58. .replace(/_plus/g, '+')
  59. .replace(/_minus/g, '-')
  60. .replace(/_star/g, '*')
  61. .replace(/_slash/g, '/')
  62. .replace(/_backslash/g, '\\')
  63. .replace(/_tild/g, '~')
  64. .replace(/_percent/g, '%')
  65. .replace(/_gt/g, '>')
  66. .replace(/_lt/g, '<')
  67. .replace(/_eq/g, '=')
  68. .replace(/_comma/g, ',')
  69. .replace(/_at/g, '@');
  70. }
  71. function addElement (array, el) {
  72. if (typeof el === 'undefined') {
  73. return;
  74. }
  75. if (array.indexOf(el) === -1) {
  76. array.push(el);
  77. }
  78. }
  79. function removeElement (array, el) {
  80. var i = array.indexOf(el);
  81. if (i !== -1) {
  82. array.splice(i, 1);
  83. }
  84. }
  85. function extend (target, source) {
  86. Object.keys(source).forEach(function (key) {
  87. target[key] = source[key];
  88. });
  89. return target;
  90. }
  91. function extendWithMethods (target, source) {
  92. Object.keys(source).forEach(function (key) {
  93. installMethodOfJsObject(target, key, source[key]);
  94. });
  95. return target;
  96. }
  97. function deleteKeysFrom (keys, obj) {
  98. keys.forEach(function (each) {
  99. delete obj[each];
  100. });
  101. }
  102. return {
  103. deleteKeysFrom: deleteKeysFrom,
  104. extendWithMethods: extendWithMethods,
  105. extend: extend,
  106. removeElement: removeElement,
  107. addElement: addElement,
  108. js2st: js2st,
  109. st2js: st2js,
  110. declareJsMethod: declareJsMethod,
  111. installMethodOfJsObject: installMethodOfJsObject,
  112. inherits: inherits
  113. }
  114. });