Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / router / src / deps / stringMagic.js
Size: Mime:
import qs from 'query-string'
import { isNil, isString, isArray, isObj } from 'exotic'
import { keys, stringify } from 'chain-able-boost'

const MATCH_NON_ALPHANUMERIC = /\w+/gim
const stripNonAlphaNumeric = x => {
  if (isString(x)) {
    return x.replace(MATCH_NON_ALPHANUMERIC, '')
  } else {
    return ''
  }
}
// @todo 1 for component 1 for uri
// (uri is for absolute url, component is for parts of url)
function isURIEncoded(str) {
  if (isString(str) === false) {
    return false
  }

  const decoded = decodeURIComponent(str)

  if (decoded === str) {
    return false
  } else {
    return true
  }
}

/**
 * @todo one of the final todos is `{}` around nested objects
 */
function fromObjToStringifiedWithoutQuotes(to, first = true) {
  // const stringified = stringify(to)
  // const withoutQuotes = stringified.replace(/\"([^(\")"]+)\":/g, '$1:')
  // recursive tail call object serialization according to JSON standard
  // ...but without quotes around the keys
  const IS_ARRAY = isArray(to)
  const separator = first === true ? '=' : ':'

  // top level entries should be & for routing, nested is for parsing
  const symbol = IS_ARRAY || first === false ? ',' : '&'

  const result = keys(to)
    .map(key => {
      // prevent going too deep
      const value =
        first === false
          ? // @todo if not primitive at not top value...
        // @example if this is bool don't want stringy bool
          stringify(to[key])
          : // only serialize objects
          isObj(to[key])
            ? fromObjToStringifiedWithoutQuotes(to[key], false)
            : to[key]

      return IS_ARRAY === true || isNil(value) || isNil(to[key])
        ? value
        : `${key}${separator}${value}`
    })
    .filter(value => value !== undefined)
    .join(symbol)

  if (IS_ARRAY === true) {
    return `[${result}]`
  } else if (first === false) {
    return `{${result}}`
  } else {
    return result
  }
}
function fromObjToSearchParamsString(to) {
  const params = new URLSearchParams(window.location.search)

  // const IS_ARRAY = isArray(to)
  Object.keys(to).forEach(key => {
    const value = to[key]
    params.set(key, value)
  })

  return params
}

// @note - this slices first and last off
// fromObjToGoodLookingParams
function goodLookingStringify(to) {
  if (isObj(to) === false) {
    return qs.stringify(to)
  }

  // const stringified = stringify(to).replace(/\\/gmi, '')
  const stringified = fromObjToStringifiedWithoutQuotes(to)

  // [eh] => eh
  // {eh: []} => eh: []
  // ...
  // @note - this ends up making it insane
  // const prettier = stringified.slice(1, stringified.length - 1)
  // console.log('___goodLookingStringify___', prettier)
  // return prettier

  return stringified
  // return qs.stringify(prettier)
  // return fromObjToSearchParamsString(to)
  // return fromObjToStringifiedWithoutQuotes(to)
}
/**
 * @todo @perf @dedupe
 * @description how many times have I/anyone written this
 */
const parseJSON = x => {
  try {
    const parsed = JSON.parse(x)
    return parsed
  } catch (syntaxException) {
    return x
  }
}

export { parseJSON, stripNonAlphaNumeric, isURIEncoded, goodLookingStringify }