vue2-utils

0.1.51 • Public • Published

Vue2Utils

Vue2Utils: Common Javascript functions as mixins

Install

    npm install vue2-utils --save

Usage

1. Import any function as mixin

import {function-name} from 'vue2-utils';

2. Use it inside your components

import {today} from 'vue2-utils';

export default {
    mixins: [today],
    methods: {
        ...
}

2. Use all the functions

//not recommended

import everything from 'vue2-utils';

export default {
    mixins: [everything],
    methods: {
        ...
}

*3. Use it anywhere in your Vue app!

if(this.empty([])) {
}

List of functions

Array related

  • is_array check if value is an Array type

    • Params: value
  • is_object check if value is an Object type

    • Params: value
  • is_scalar checks if value is a primitive like number, string, etc

    • Params: value
  • to_array converts any value into an array if not already

    • Params: value
  • obj_values returns all the values of a object

    • Params: obj
  • for_each iterates over an array or hash using callback. quits if callback returns false

    • Params: value, fn
  • clone Deep copies an object replacing all old references

    • Params: obj
  • json_safe_stringify Stringifies an object ignoring circular references

    • Params: o
  • array_filter_items returns only matching items (if array item matches, or hash has any matching values)

    • Params: arr, match, exact = false, ignore_case = true
  • array_merge merges multiple arrays into one

    • Params: ...arrays
  • array_unique return unique elements of array

    • Params: array
  • where Looks through each value in the list, returning all values that matches the key-value pairs listed in properties.

    • Params: arr, match
  • randomize randomizes the array

    • Params: array
  • pluck returns only a set key from a hash

    • Params: obj, ...keys
  • in_array checks if a value exists inside array

    • Params: needle, haystack, ignore_case = false
  • toggle_array_item removes or inserts an array item

    • Params: needle, haystack, ignore_case = false
  • sort_by_key sort an array by key

    • Params: array, key, reverse
  • group_by groups an array by key

    • Params: array, key
  • chunk chunks array in size of len

    • Params: arr, len
  • obj_extend

    • Params: dest, ...objects
  • obj_set set an obj using dot path notation

    • Params: object, path, value
  • obj_get gets an obj using dot path notation

    • Params: object, path
  • obj_init only sets an obj if there is no existing value

    • Params: object, path, value, type = null
  • obj_vue_set

    • Params: o, key, value
  • obj_flat flattens a object (optionally applying a filter function to values/key before adding)

    • Params: data, filter = null
  • array_clean removes empty elements of an array or non empty keys from hash

    • Params: actual
  • remove_item removes an item from array or object (modes: 'ignore_case' = for scalar array, 'fuzzy' = for objects [compares using JSON.stringify each object))

    • Params: needle, haystack, mode = null
  • equals deep compares two objects for equality

    • Params: x, y

String related

  • lines breaks long text into lines (delimited by \r?\n)

    • Params: string
  • sentences breaks long text into sentences (by period, question mark, etc)

    • Params: str
  • words break text into words

    • Params: str
  • single_line combine array into long text

    • Params: lines, delim = ''
  • ascii remove non ascii chars from a string (utf8)

    • Params: str
  • slugify turns a string into slug format

    • Params: str
  • password generates random password

    • Params: len
  • kebab converts string (including camelCase) into kebab case

    • Params: str, delim = '-'
  • camel turns string to camelCase

    • Params: str
  • remove_parens remove parenthesis from text

    • Params: Input
  • str_replace replace placeholders in string

    • Params: str, replacements
  • str_equals compares two strings for equality with/without case-sensitivity

    • Params: str1, str2, ignore_case = true
  • empty checks if a value is empty incl. array and hash

    • Params: value
  • ucfirst capitalizes the first letter of string

    • Params: string
  • ucwords capitalizes all words

    • Params: string
  • trim trims a string from both sides

    • Params: value, char = "\s\s*"
  • ltrim trims a string from left

    • Params: value, char = "\s\s*"
  • rtrim trims a string from right

    • Params: value, char = "\s\s*"
  • truncate truncates a string at maxlen (adds ellipses if truncated)

    • Params: str, maxlen = 20, html = false
  • stop_words returns a promise that resolves with most common stop words

    • Params: none
  • html2text converts html to plain text

    • Params: html
  • quotemeta add backslashes to a string for quoting regex, etc

    • Params: text

Internet related

  • is_url checks if value is URL

    • Params: url
  • google creates a google search link for a keyword

    • Params: keyword, lucky = false
  • http_get performs a simple get request

    • Params: url
  • parse_query parses a query string into hash

    • Params: queryString = null
  • query_get_param parses current url for named param

    • Params: name, fallback = ''
  • get_hash_value parses current url for param in hash (part after #)

    • Params: key
  • image_size returns a promise that gives width and height of images

    • Params: src
  • domain return the TLD for any url or hostname

    • Params: url, tld = true
  • check_email checks if an email address is valid or not (with details)

    • Params: email
  • get_youtube_id returns youtube-id from any youtube url

    • Params: url
  • get_youtube_thumb gets the thumbnail of a youtube video

    • Params: url, hq = true
  • get_thumb generates a thumbnail from any url including youtube videos

    • Params: url, width = 200, height = ''
  • random_photo returns a random photo for placeholder

    • Params: width = 300, height = 200
  • random_user_data returns a random user data for testing

    • Params: none

Number related

  • to_num always returns a number or 0 (fix NaN issues)

    • Params: value
  • random returns a random number between min and max

    • Params: min, max

File related

  • basename returns the basename of a file

    • Params: str
  • extension return the extension of a file

    • Params: filename
  • noop this function does nothing

    • Params: none
  • hash_code Generate a Hash from string

    • Params: str
  • today returns today's date in dd/mm/yy

    • Params: date = null, delim = '/'
  • to_date parses a date string into Javascript Date

    • Params: str
  • hms converts seconds in hh:mm:ss

    • Params: secs
  • parse_date parses any time into a date

    • Params: time, tz = false
  • time_ago human readable relative time

    • Params: time, tz = false
  • debounce makes a debounced version of a function, let debounced = debounce(() => console.log(111), 1500); debounced(); debounced();

    • Params: func, wait, immediate = false
  • is_debug checks if debug mode is on

    • Params: none
  • browser return browser's name

    • Params: none
  • redirect redirect to another page using GET or POST with data

    • Params: url, target = '_self', method = 'get', data = {}
  • clipboard_copy copies text to clipboard (must be initiated by a user click)

    • Params: text
  • set_cookie sets a cookie

    • Params: name, value, days = 30
  • get_cookie gets a cookie by name

    • Params: name
  • erase_cookie deletes a cookie by name

    • Params: name
  • save_object saves an object in localStorage

    • Params: key, value
  • load_object gets an object from localStorage

    • Params: key
  • load_script loads a script and returns promise

    • Params: src
  • debug writes arguments to console.log

    • Params: ...args
  • is_mobile_device

    • Params: width = 768, checkAgent = false
  • mime_type

    • Params: filename

Bootstrap related

  • mobile_text wraps text in html that changes based on browser resolution

    • Params: normal, mobile, split = 'md', block = 'inline'
  • fa_icon return html for a font awesome icon

    • Params: value, fw = false

Contributing

Contributions are welcome

Build Setup

# install dependencies
npm install
 
# serve with hot reload at localhost:8080
npm run dev
 
# build for production with minification
npm run build

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.1.515latest

Version History

VersionDownloads (Last 7 Days)Published
0.1.515
0.1.501
0.1.491
0.1.480
0.1.470
0.1.460
0.1.450
0.1.440
0.1.430
0.1.421
0.1.411
0.1.400
0.1.390
0.1.380
0.1.371
0.1.361
0.1.350
0.1.340
0.1.330
0.1.320
0.1.310
0.1.301
0.1.290
0.1.280
0.1.270
0.1.260
0.1.251
0.1.240
0.1.230
0.1.221
0.1.210
0.1.201
0.1.190
0.1.180
0.1.170
0.1.160
0.1.151
0.1.140
0.1.130
0.1.120
0.1.111
0.1.100
0.1.90
0.1.80
0.1.71
0.1.60
0.1.51
0.1.41
0.1.30
0.1.21
0.1.11
0.1.00
0.0.91
0.0.80
0.0.71
0.0.61
0.0.50
0.0.40
0.0.30
0.0.20
0.0.10

Package Sidebar

Install

npm i vue2-utils

Weekly Downloads

20

Version

0.1.51

License

MIT

Unpacked Size

164 kB

Total Files

7

Last publish

Collaborators

  • san-kumar