Sha256: 33ac27a69bae62d5e890024081a10009e53b38b2a7273008f45cf4977e8a8f22

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module = angular.module("mb.filters", [])

module.filter "humanSize", -> (bytes) ->
  sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
  n = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10)
  scaledSize = (bytes / Math.pow(1024, n)).toFixed(0)

  "#{scaledSize} #{sizes[n]}"

module.filter "jsonDocument", -> (document) ->
  str = JSON.stringify(document, undefined, 2)

  # Replace for the ObjectId
  str = str.replace(/_id": {\s+"\$oid":\s+"(\w+)"\s+}/gm, '_id": ObjectId("$1")')

  syntaxHighlight = (json) ->
    json = json.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
    json.replace /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, (match) ->
      cls = "number"
      if /^"/.test(match)
        if /:$/.test(match)
          # Remove quotes from a key
          match = match.replace(/^"/, "").replace(/":$/, ":")

          cls = "key"
        else
          cls = "string"
      else if /true|false/.test(match)
        cls = "boolean"
      else
        cls = "null" if /null/.test(match)

      "<span class=\"#{cls}\">#{match}</span>"

  "<pre>#{syntaxHighlight(str)}</pre>"

module.filter "collectionsPath", ->
  (database = {}) ->
    dbName = database.name || ":dbName"

    "/databases/#{dbName}/collections"

module.filter "documentsPath", (collectionsPathFilter) ->
  (collection = {}) ->
    dbName = collection.dbName || ":dbName"
    collectionName = collection.name || ":collectionName"

    "#{collectionsPathFilter(name: dbName)}/#{collectionName}/documents"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongo_browser-0.2.0.rc2 app/assets/javascripts/app/filters.js.coffee