escape.js 586 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. const encoded = {
  12. '&': '&',
  13. '<': '&lt;',
  14. '>': '&gt;',
  15. '/': '&#x2F;',
  16. '\'': '&#x27;',
  17. '"': '&quot;'
  18. };
  19. export default function escape(str
  20. /*: string*/
  21. )
  22. /*: string*/
  23. {
  24. return str.replace(/[&<>\/'"]/g, function (char) {
  25. return encoded[char];
  26. });
  27. }