From 2e04bd6ea9dbc0513f4f65be4f8894e97609dcf9 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 1 Aug 2016 16:21:11 -0700 Subject: [PATCH] Convert window.coffee and clipboard.coffee to javascript --- src/clipboard.coffee | 61 -------------------------------------- src/clipboard.js | 70 ++++++++++++++++++++++++++++++++++++++++++++ src/window.coffee | 27 ----------------- src/window.js | 30 +++++++++++++++++++ 4 files changed, 100 insertions(+), 88 deletions(-) delete mode 100644 src/clipboard.coffee create mode 100644 src/clipboard.js delete mode 100644 src/window.coffee create mode 100644 src/window.js diff --git a/src/clipboard.coffee b/src/clipboard.coffee deleted file mode 100644 index 9511bdda9..000000000 --- a/src/clipboard.coffee +++ /dev/null @@ -1,61 +0,0 @@ -crypto = require 'crypto' -clipboard = require './safe-clipboard' - -# Extended: Represents the clipboard used for copying and pasting in Atom. -# -# An instance of this class is always available as the `atom.clipboard` global. -# -# ## Examples -# -# ```coffee -# atom.clipboard.write('hello') -# -# console.log(atom.clipboard.read()) # 'hello' -# ``` -module.exports = -class Clipboard - constructor: -> - @reset() - - reset: -> - @metadata = null - @signatureForMetadata = null - - # Creates an `md5` hash of some text. - # - # * `text` A {String} to hash. - # - # Returns a hashed {String}. - md5: (text) -> - crypto.createHash('md5').update(text, 'utf8').digest('hex') - - # Public: Write the given text to the clipboard. - # - # The metadata associated with the text is available by calling - # {::readWithMetadata}. - # - # * `text` The {String} to store. - # * `metadata` (optional) The additional info to associate with the text. - write: (text, metadata) -> - @signatureForMetadata = @md5(text) - @metadata = metadata - clipboard.writeText(text) - - # Public: Read the text from the clipboard. - # - # Returns a {String}. - read: -> - clipboard.readText() - - # Public: Read the text from the clipboard and return both the text and the - # associated metadata. - # - # Returns an {Object} with the following keys: - # * `text` The {String} clipboard text. - # * `metadata` The metadata stored by an earlier call to {::write}. - readWithMetadata: -> - text = @read() - if @signatureForMetadata is @md5(text) - {text, @metadata} - else - {text} diff --git a/src/clipboard.js b/src/clipboard.js new file mode 100644 index 000000000..34f6b1f83 --- /dev/null +++ b/src/clipboard.js @@ -0,0 +1,70 @@ +/** @babel */ + +import crypto from 'crypto' +import clipboard from './safe-clipboard' + +// Extended: Represents the clipboard used for copying and pasting in Atom. +// +// An instance of this class is always available as the `atom.clipboard` global. +// +// ## Examples +// +// ```coffee +// atom.clipboard.write('hello') +// +// console.log(atom.clipboard.read()) # 'hello' +// ``` +export default class Clipboard { + constructor () { + this.reset() + } + + reset () { + this.metadata = null + this.signatureForMetadata = null + } + + // Creates an `md5` hash of some text. + // + // * `text` A {String} to hash. + // + // Returns a hashed {String}. + md5 (text) { + return crypto.createHash('md5').update(text, 'utf8').digest('hex') + } + + // Public: Write the given text to the clipboard. + // + // The metadata associated with the text is available by calling + // {::readWithMetadata}. + // + // * `text` The {String} to store. + // * `metadata` (optional) The additional info to associate with the text. + write (text, metadata) { + this.signatureForMetadata = this.md5(text) + this.metadata = metadata + clipboard.writeText(text) + } + + // Public: Read the text from the clipboard. + // + // Returns a {String}. + read () { + return clipboard.readText() + } + + // Public: Read the text from the clipboard and return both the text and the + // associated metadata. + // + // Returns an {Object} with the following keys: + // * `text` The {String} clipboard text. + // * `metadata` The metadata stored by an earlier call to {::write}. + readWithMetadata () { + let text = this.read() + if (this.signatureForMetadata === this.md5(text)) { + return {text, metadata: this.metadata} + } else { + return {text} + } + } +} diff --git a/src/window.coffee b/src/window.coffee deleted file mode 100644 index 9554218ca..000000000 --- a/src/window.coffee +++ /dev/null @@ -1,27 +0,0 @@ -# Public: Measure how long a function takes to run. -# -# description - A {String} description that will be logged to the console when -# the function completes. -# fn - A {Function} to measure the duration of. -# -# Returns the value returned by the given function. -window.measure = (description, fn) -> - start = Date.now() - value = fn() - result = Date.now() - start - console.log description, result - value - -# Public: Create a dev tools profile for a function. -# -# description - A {String} description that will be available in the Profiles -# tab of the dev tools. -# fn - A {Function} to profile. -# -# Returns the value returned by the given function. -window.profile = (description, fn) -> - measure description, -> - console.profile(description) - value = fn() - console.profileEnd(description) - value diff --git a/src/window.js b/src/window.js new file mode 100644 index 000000000..c4f28ba96 --- /dev/null +++ b/src/window.js @@ -0,0 +1,30 @@ +// Public: Measure how long a function takes to run. +// +// description - A {String} description that will be logged to the console when +// the function completes. +// fn - A {Function} to measure the duration of. +// +// Returns the value returned by the given function. +window.measure = function (description, fn) { + let start = Date.now() + let value = fn() + let result = Date.now() - start + console.log(description, result) + return value +} + +// Public: Create a dev tools profile for a function. +// +// description - A {String} description that will be available in the Profiles +// tab of the dev tools. +// fn - A {Function} to profile. +// +// Returns the value returned by the given function. +window.profile = function (description, fn) { + window.measure(description, function () { + console.profile(description) + let value = fn() + console.profileEnd(description) + return value + }) +}