utils.js 17.4 KB
Newer Older
R
Ron 已提交
1 2 3 4 5
import Im from "immutable"

import camelCase from "lodash/camelCase"
import upperFirst from "lodash/upperFirst"
import _memoize from "lodash/memoize"
6
import find from "lodash/find"
R
Ron 已提交
7 8 9
import some from "lodash/some"
import eq from "lodash/eq"
import { memoizedSampleFromSchema, memoizedCreateXMLExample } from "core/plugins/samples/fn"
10
import win from "./window"
R
Ron 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

const DEFAULT_REPONSE_KEY = "default"

export const isImmutable = (maybe) => Im.Iterable.isIterable(maybe)

export function objectify (thing) {
  if(!isObject(thing))
    return {}
  if(isImmutable(thing))
    return thing.toObject()
  return thing
}

export function arrayify (thing) {
  if(!thing)
    return []

  if(thing.toArray)
    return thing.toArray()

  return normalizeArray(thing)
}

export function fromJSOrdered (js) {
  if(isImmutable(js))
    return js // Can't do much here

38 39 40
  if (js instanceof win.File)
    return js

R
Ron 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  return !isObject(js) ? js :
    Array.isArray(js) ?
      Im.Seq(js).map(fromJSOrdered).toList() :
      Im.Seq(js).map(fromJSOrdered).toOrderedMap()
}

export function bindToState(obj, state) {
	var newObj = {}
	Object.keys(obj)
  .filter(key => typeof obj[key] === "function")
  .forEach(key => newObj[key] = obj[key].bind(null, state))
	return newObj
}

export function normalizeArray(arr) {
  if(Array.isArray(arr))
    return arr
  return [arr]
}

export function isFn(fn) {
  return typeof fn === "function"
}

export function isObject(obj) {
  return !!obj && typeof obj === "object"
}

export function isFunc(thing) {
  return typeof(thing) === "function"
}

export function isArray(thing) {
  return Array.isArray(thing)
}

// I've changed memoize libs more than once, so I'm using this a way to make that simpler
export const memoize = _memoize

export function objMap(obj, fn) {
  return Object.keys(obj).reduce((newObj, key) => {
    newObj[key] = fn(obj[key], key)
    return newObj
  }, {})
}

export function objReduce(obj, fn) {
  return Object.keys(obj).reduce((newObj, key) => {
    let res = fn(obj[key], key)
    if(res && typeof res === "object")
91
      Object.assign(newObj, res)
R
Ron 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return newObj
  }, {})
}

// Redux middleware that exposes the system to async actions (like redux-thunk, but with out system instead of (dispatch, getState)
export function systemThunkMiddleware(getSystem) {
  return ({ dispatch, getState }) => { // eslint-disable-line no-unused-vars
    return next => action => {
      if (typeof action === "function") {
        return action(getSystem())
      }

      return next(action)
    }
  }
}

export const errorLog = getSystem => () => next => action => {
  try{
    next( action )
  }
  catch( e ) {
    getSystem().errActions.newThrownErr( e, action )
  }
}

export function defaultStatusCode ( responses ) {
  let codes = responses.keySeq()
  return codes.contains(DEFAULT_REPONSE_KEY) ? DEFAULT_REPONSE_KEY : codes.filter( key => (key+"")[0] === "2").sort().first()
}


/**
 * Returns an Immutable List, safely
 * @param {Immutable.Iterable} iterable the iterable to get the key from
 * @param {String|[String]} key either an array of keys, or a single key
 * @returns {Immutable.List} either iterable.get(keys) or an empty Immutable.List
 */
export function getList(iterable, keys) {
  if(!Im.Iterable.isIterable(iterable)) {
    return Im.List()
  }
  let val = iterable.getIn(Array.isArray(keys) ? keys : [keys])
  return Im.List.isList(val) ? val : Im.List()
}

// Adapted from http://stackoverflow.com/a/2893259/454004
// Note: directly ported from CoffeeScript
export function formatXml (xml) {
K
Kyle Shockey 已提交
141
  var contexp, fn, formatted, indent, l, lastType, len, lines, ln, reg, transitions, wsexp
R
Ron 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  reg = /(>)(<)(\/*)/g
  wsexp = /[ ]*(.*)[ ]+\n/g
  contexp = /(<.+>)(.+\n)/g
  xml = xml.replace(/\r\n/g, "\n").replace(reg, "$1\n$2$3").replace(wsexp, "$1\n").replace(contexp, "$1\n$2")
  formatted = ""
  lines = xml.split("\n")
  indent = 0
  lastType = "other"
  transitions = {
    "single->single": 0,
    "single->closing": -1,
    "single->opening": 0,
    "single->other": 0,
    "closing->single": 0,
    "closing->closing": -1,
    "closing->opening": 0,
    "closing->other": 0,
    "opening->single": 1,
    "opening->closing": 0,
    "opening->opening": 1,
    "opening->other": 1,
    "other->single": 0,
    "other->closing": -1,
    "other->opening": 0,
    "other->other": 0
  }
  fn = function(ln) {
K
Kyle Shockey 已提交
169
    var fromTo, key, padding, type, types, value
R
Ron 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    types = {
      single: Boolean(ln.match(/<.+\/>/)),
      closing: Boolean(ln.match(/<\/.+>/)),
      opening: Boolean(ln.match(/<[^!?].*>/))
    }
    type = ((function() {
      var results
      results = []
      for (key in types) {
        value = types[key]
        if (value) {
          results.push(key)
        }
      }
      return results
    })())[0]
    type = type === void 0 ? "other" : type
    fromTo = lastType + "->" + type
    lastType = type
    padding = ""
    indent += transitions[fromTo]
    padding = ((function() {
K
Kyle Shockey 已提交
192 193
      /* eslint-disable no-unused-vars */
      var m, ref1, results, j
R
Ron 已提交
194 195 196 197
      results = []
      for (j = m = 0, ref1 = indent; 0 <= ref1 ? m < ref1 : m > ref1; j = 0 <= ref1 ? ++m : --m) {
        results.push("  ")
      }
K
Kyle Shockey 已提交
198
      /* eslint-enable no-unused-vars */
R
Ron 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
      return results
    })()).join("")
    if (fromTo === "opening->closing") {
      formatted = formatted.substr(0, formatted.length - 1) + ln + "\n"
    } else {
      formatted += padding + ln + "\n"
    }
  }
  for (l = 0, len = lines.length; l < len; l++) {
    ln = lines[l]
    fn(ln)
  }
  return formatted
}


/**
 * Adapted from http://github.com/asvd/microlight
 * @copyright 2016 asvd <heliosframework@gmail.com>
 */
export function highlight (el) {
  const MAX_LENGTH = 5000
  var
    _document = document,
    appendChild = "appendChild",
K
Kyle Shockey 已提交
224
    test = "test"
R
Ron 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

  if (!el) return ""
  if (el.textContent.length > MAX_LENGTH) { return el.textContent }

  var reset = function(el) {
    var text = el.textContent,
      pos = 0,       // current position
      next1 = text[0], // next character
      chr = 1,       // current character
      prev1,           // previous character
      prev2,           // the one before the previous
      token =          // current token content
        el.innerHTML = "",  // (and cleaning the node)

    // current token type:
    //  0: anything else (whitespaces / newlines)
    //  1: operator or brace
    //  2: closing braces (after which '/' is division not regex)
    //  3: (key)word
    //  4: regex
    //  5: string starting with "
    //  6: string starting with '
    //  7: xml comment  <!-- -->
    //  8: multiline comment /* */
    //  9: single-line comment starting with two slashes //
    // 10: single-line comment starting with hash #
      tokenType = 0,

    // kept to determine between regex and division
      lastTokenType,
    // flag determining if token is multi-character
      multichar,
K
Kyle Shockey 已提交
257
      node
R
Ron 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

    // running through characters and highlighting
    while (prev2 = prev1,
      // escaping if needed (with except for comments)
      // pervious character will not be therefore
      // recognized as a token finalize condition
      prev1 = tokenType < 7 && prev1 == "\\" ? 1 : chr
      ) {
      chr = next1
      next1=text[++pos]
      multichar = token.length > 1

      // checking if current token should be finalized
      if (!chr || // end of content
          // types 9-10 (single-line comments) end with a
          // newline
        (tokenType > 8 && chr == "\n") ||
        [ // finalize conditions for other token types
          // 0: whitespaces
          /\S/[test](chr),  // merged together
          // 1: operators
          1,                // consist of a single character
          // 2: braces
          1,                // consist of a single character
          // 3: (key)word
          !/[$\w]/[test](chr),
          // 4: regex
          (prev1 == "/" || prev1 == "\n") && multichar,
          // 5: string with "
          prev1 == "\"" && multichar,
          // 6: string with '
          prev1 == "'" && multichar,
          // 7: xml comment
          text[pos-4]+prev2+prev1 == "-->",
          // 8: multiline comment
          prev2+prev1 == "*/"
        ][tokenType]
      ) {
        // appending the token to the result
        if (token) {
          // remapping token type into style
          // (some types are highlighted similarly)
          el[appendChild](
            node = _document.createElement("span")
          ).setAttribute("style", [
            // 0: not formatted
            "color: #555; font-weight: bold;",
            // 1: keywords
            "",
            // 2: punctuation
            "",
            // 3: strings and regexps
            "color: #555;",
            // 4: comments
            ""
          ][
            // not formatted
            !tokenType ? 0 :
              // punctuation
              tokenType < 3 ? 2 :
                // comments
                tokenType > 6 ? 4 :
                  // regex and strings
                  tokenType > 3 ? 3 :
                    // otherwise tokenType == 3, (key)word
                    // (1 if regexp matches, 0 otherwise)
                    + /^(a(bstract|lias|nd|rguments|rray|s(m|sert)?|uto)|b(ase|egin|ool(ean)?|reak|yte)|c(ase|atch|har|hecked|lass|lone|ompl|onst|ontinue)|de(bugger|cimal|clare|f(ault|er)?|init|l(egate|ete)?)|do|double|e(cho|ls?if|lse(if)?|nd|nsure|num|vent|x(cept|ec|p(licit|ort)|te(nds|nsion|rn)))|f(allthrough|alse|inal(ly)?|ixed|loat|or(each)?|riend|rom|unc(tion)?)|global|goto|guard|i(f|mp(lements|licit|ort)|n(it|clude(_once)?|line|out|stanceof|t(erface|ernal)?)?|s)|l(ambda|et|ock|ong)|m(icrolight|odule|utable)|NaN|n(amespace|ative|ext|ew|il|ot|ull)|o(bject|perator|r|ut|verride)|p(ackage|arams|rivate|rotected|rotocol|ublic)|r(aise|e(adonly|do|f|gister|peat|quire(_once)?|scue|strict|try|turn))|s(byte|ealed|elf|hort|igned|izeof|tatic|tring|truct|ubscript|uper|ynchronized|witch)|t(emplate|hen|his|hrows?|ransient|rue|ry|ype(alias|def|id|name|of))|u(n(checked|def(ined)?|ion|less|signed|til)|se|sing)|v(ar|irtual|oid|olatile)|w(char_t|hen|here|hile|ith)|xor|yield)$/[test](token)
            ])

          node[appendChild](_document.createTextNode(token))
        }

        // saving the previous token type
        // (skipping whitespaces and comments)
        lastTokenType =
          (tokenType && tokenType < 7) ?
            tokenType : lastTokenType

        // initializing a new token
        token = ""

        // determining the new token type (going up the
        // list until matching a token type start
        // condition)
        tokenType = 11
        while (![
          1,                   //  0: whitespace
                               //  1: operator or braces
R
RVKen 已提交
346
          /[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr),   // eslint-disable-line no-useless-escape
R
Ron 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
          /[\])]/[test](chr),  //  2: closing brace
          /[$\w]/[test](chr),  //  3: (key)word
          chr == "/" &&        //  4: regex
            // previous token was an
            // opening brace or an
            // operator (otherwise
            // division, not a regex)
          (lastTokenType < 2) &&
            // workaround for xml
            // closing tags
          prev1 != "<",
          chr == "\"",          //  5: string with "
          chr == "'",          //  6: string with '
                               //  7: xml comment
          chr+next1+text[pos+1]+text[pos+2] == "<!--",
          chr+next1 == "/*",   //  8: multiline comment
          chr+next1 == "//",   //  9: single-line comment
          chr == "#"           // 10: hash-style comment
        ][--tokenType]);
      }

      token += chr
    }
  }

  return reset(el)
}

/**
 * Take an immutable map, and convert to a list.
 * Where the keys are merged with the value objects
 * @param {Immutable.Map} map, the map to convert
 * @param {String} key the key to use, when merging the `key`
 * @returns {Immutable.List}
 */
export function mapToList(map, keyNames="key", collectedKeys=Im.Map()) {
  if(!Im.Map.isMap(map) || !map.size) {
    return Im.List()
  }

  if(!Array.isArray(keyNames)) {
    keyNames = [ keyNames ]
  }

  if(keyNames.length < 1) {
    return map.merge(collectedKeys)
  }

  // I need to avoid `flatMap` from merging in the Maps, as well as the lists
  let list = Im.List()
  let keyName = keyNames[0]
  for(let entry of map.entries()) {
    let [key, val] = entry
    let nextList = mapToList(val, keyNames.slice(1), collectedKeys.set(keyName, key))
    if(Im.List.isList(nextList)) {
      list = list.concat(nextList)
    } else {
      list = list.push(nextList)
    }
  }

  return list
}

// PascalCase, aka UpperCamelCase
export function pascalCase(str) {
  return upperFirst(camelCase(str))
}

// Remove the ext of a filename, and pascalCase it
export function pascalCaseFilename(filename) {
  return pascalCase(filename.replace(/\.[^./]*$/, ""))
}

// Check if ...
// - new props
// - If immutable, use .is()
// - if in explicit objectList, then compare using _.eq
// - else use ===
export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {

  if(Object.keys(props).length !== Object.keys(nextProps).length) {
    return true
  }

  return (
    some(props, (a, name) => {
      if(ignoreList.includes(name)) {
        return false
      }
      let b = nextProps[name]

      if(Im.Iterable.isIterable(a)) {
        return !Im.is(a,b)
      }

      // Not going to compare objects
      if(typeof a === "object" && typeof b === "object") {
        return false
      }

      return a !== b
    })
    || objectList.some( objectPropName => !eq(props[objectPropName], nextProps[objectPropName])))
}

453 454
export const validateNumber = ( val ) => {
  if ( !/^-?\d+(\.?\d+)?$/.test(val)) {
K
Kyle Shockey 已提交
455 456 457 458
    return "Value must be a number"
  }
}

459
export const validateInteger = ( val ) => {
J
Jeff Weeks 已提交
460
  if ( !/^-?\d+$/.test(val)) {
461
    return "Value must be an integer"
K
Kyle Shockey 已提交
462 463
  }
}
R
Ron 已提交
464

465
export const validateFile = ( val ) => {
O
Owen Conti 已提交
466
  if ( val && !(val instanceof win.File) ) {
467 468 469 470
    return "Value must be a file"
  }
}

R
Ron 已提交
471 472 473 474 475 476 477
// validation of parameters before execute
export const validateParam = (param, isXml) => {
  let errors = []
  let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value")
  let required = param.get("required")
  let type = param.get("type")

478 479 480
  let stringCheck = type === "string" && !value
  let arrayCheck = type === "array" && Array.isArray(value) && !value.length
  let listCheck = type === "array" && Im.List.isList(value) && !value.count()
481 482 483
  let fileCheck = type === "file" && !(value instanceof win.File)

  if ( required && (stringCheck || arrayCheck || listCheck || fileCheck) ) {
R
Ron 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
    errors.push("Required field is not provided")
    return errors
  }

  if ( type === "number" ) {
    let err = validateNumber(value)
    if (!err) return errors
    errors.push(err)
  } else if ( type === "integer" ) {
    let err = validateInteger(value)
    if (!err) return errors
    errors.push(err)
  } else if ( type === "array" ) {
    let itemType

    if ( !value.count() ) { return errors }

    itemType = param.getIn(["items", "type"])

    value.forEach((item, index) => {
      let err

      if (itemType === "number") {
        err = validateNumber(item)
      } else if (itemType === "integer") {
        err = validateInteger(item)
      }

      if ( err ) {
        errors.push({ index: index, error: err})
      }
    })
516 517 518 519
  } else if ( type === "file" ) {
    let err = validateFile(value)
    if (!err) return errors
    errors.push(err)
R
Ron 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533
  }

  return errors
}

export const getSampleSchema = (schema, contentType="", config={}) => {
  if (/xml/.test(contentType)) {
    if (!schema.xml || !schema.xml.name) {
      schema.xml = schema.xml || {}

      if (schema.$$ref) {
        let match = schema.$$ref.match(/\S*\/(\S+)$/)
        schema.xml.name = match[1]
      } else if (schema.type || schema.items || schema.properties || schema.additionalProperties) {
K
Kyle Shockey 已提交
534
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- XML example cannot be generated -->"
R
Ron 已提交
535 536 537 538 539 540 541 542 543
      } else {
        return null
      }
    }
    return memoizedCreateXMLExample(schema, config)
  }

  return JSON.stringify(memoizedSampleFromSchema(schema, config), null, 2)
}
544 545 546 547 548 549

export const parseSeach = () => {
  let map = {}
  let search = window.location.search

  if ( search != "" ) {
K
Kyle Shockey 已提交
550
    let params = search.substr(1).split("&")
551 552

    for (let i in params) {
K
Kyle Shockey 已提交
553 554
      i = params[i].split("=")
      map[decodeURIComponent(i[0])] = decodeURIComponent(i[1])
555 556 557
    }
  }

K
Kyle Shockey 已提交
558
  return map
K
Kyle Shockey 已提交
559
}
A
Anna Bodnia 已提交
560 561

export const btoa = (str) => {
S
shockey 已提交
562
  let buffer
A
Anna Bodnia 已提交
563 564

  if (str instanceof Buffer) {
S
shockey 已提交
565
    buffer = str
A
Anna Bodnia 已提交
566
  } else {
S
shockey 已提交
567
    buffer = new Buffer(str.toString(), "utf-8")
A
Anna Bodnia 已提交
568 569
  }

S
shockey 已提交
570
  return buffer.toString("base64")
A
Anna Bodnia 已提交
571
}
A
Anna Bodnia 已提交
572 573 574 575 576 577 578

export const sorters = {
  operationsSorter: {
    alpha: (a, b) => a.get("path").localeCompare(b.get("path")),
    method: (a, b) => a.get("method").localeCompare(b.get("method"))
  }
}
579 580 581 582 583 584

export const buildFormData = (data) => {
  let formArr = []

  for (let name in data) {
    let val = data[name]
A
build  
Anna Bodnia 已提交
585
    if (val !== undefined && val !== "") {
586 587 588 589 590
      formArr.push([name, "=", encodeURIComponent(val).replace(/%20/g,"+")].join(""))
    }
  }
  return formArr.join("&")
}
591 592 593 594 595 596 597 598 599 600 601 602

export const filterConfigs = (configs, allowed) => {
    let i, filteredConfigs = {}

    for (i in configs) {
        if (allowed.indexOf(i) !== -1) {
            filteredConfigs[i] = configs[i]
        }
    }

    return filteredConfigs
}
603 604 605 606 607 608 609

// Is this really required as a helper? Perhaps. TODO: expose the system of presets.apis in docs, so we know what is supported
export const shallowEqualKeys = (a,b, keys) => {
  return !!find(keys, (key) => {
    return eq(a[key], b[key])
  })
}