1.0.4 • Published 8 years ago

martinadams-utils v1.0.4

Weekly downloads
10
License
ISC
Repository
github
Last release
8 years ago

Sample Library of Ordinary JavaScript Functions

Use files like this in your JavaScript file:

var index = require('martinadams-utils')  // Load index module, which loads submodules from 'lib' folder
var	add = index.add,
		addStrings = index.addStrings,
		...
		splitStringByCommas = index.splitStringByCommas,
		toNumber = index.toNumber

getType("This is a string")     //  Returns 'string'

add

function (a, b) {
  return a + b
}

addStrings

function (a, b) {
  return (parseFloat(a) + parseFloat(b)).toString()
}

addStringsOrNumbers

function (a, b) {
  if ((typeof a === "string") || (typeof b === "string")) {
    return (parseFloat(a) + parseFloat(b)).toString()
  } else {
    return a + b
  }
}

countIf

function (testFunc, arr) {
  return arr.filter(testFunc).length
}

each

function (func, arr) {
  for (var i = 0; i < arr.length; i++) {
    func(arr[i])
  }
}

filter

function (func, arr) {
  return arr.filter(func)
}

filterStringsWithCommas

function (str) {
  return (str.indexOf(',') > -1)
}

getType

function (thing) {
  return typeof thing
}

isEmail

function (str) {
  var re = /[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}/igm;
  if (re.test(str)) return true; else return false;
}

isNumber

function (thing) {
  return typeof thing === "number"
}

isString

function (s) {
  return typeof s === 'string'
}

isStringNumber

function (str) {
  return !isNaN(str)
}

map

function (func, arr) {
  return arr.map(func)
}

splitStringByCommas

function (str) {
  return str.split(',')
}

toNumber

function (str) {
  return parseFloat(str)
}
1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago