1.0.0 • Published 6 years ago

kis2 v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 years ago

kissJS

a lib collect some awesome method

API

Util API

countProp getType isArray isUndefined getRandomChar getRandomNum

Array API

sort composeToArrary unique reverse isPure delByVal delByIndex findIndex uniqueArrForRef

Function API

Object API

Util:

Arg:    object(Object)    all(Boolean):   if return all properties contains Symbol property and enumerable is false

Return:    (Number):   return the count object's properties

example:

  var obj = {guy: 'manx'};

  Object.defineProperty(obj, 'secret', {
    enumerable: false
  });

  var count = K.countProp(obj);
  console.log(count); // 1

  var count2 = K.countProp(obj, true);
  console.log(count2); // 2

Arg:     bar(anyType)

Return:     (String): return the type of bar

example:

  var text = 'hello';
  var type = K.getType(text);
  console.log(type); // String

Arg:     bar(anyType)

Return:   (Boolean): true or false

example

  var bar = 'I am text';
  console.log(K.isArray(bar)); // false

  var arr = [1, 2, 3];
  var ret = K.isArray(arr);
  console.log(ret); // true

Example

  var foo;
  console.log(K.isUndefined(foo)); // true

Arg:    isCapital (Boolean): use Uppercase or Lowercase

Return:    (String): a char

example:

  console.log(K.getRandomChar()); // (a random lowercase char)
  console.log(K.getRandomChar(true)); // (a random Uppercase char)

Arg:    min(Number): the base    max(Number): the max

note      if don't provide the max, but the min, default min as max and zero as min     if all are not provide, default the min is zero and the max is ten

Return:     (Number): return a number according your setting

example:

  console.log(K.getRandomNum()); // generate a number which less than ten
  console.log(K.getRandomNum(20, 50)); // generate a number which more than 20 and less than 50

Array related api:

Arg:     isIncrease(Boolean): true or false

Return:     (Array): return the arr by sort

Example

  var arr = [1, 3, 2];
  K.sort(arr, true);
  console.log(arr); // [1, 2, 3]

  K.sort(arr);
  console.log(arr); // [3, 2, 1]

Arg:    amount(Number): the compose array's element

Return:    (Array): return a array by compose

Example

  var arr = [1, 2, 3, 4, 5, 67];
  var ret = K.composeToArr(arr, 1);
  console.log(ret);  // [ [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ], [ 67 ] ]

  var ret2 = K.composeToArr(arr, 3); 
  console.log(ret2); // [ [ 1, 2, 3 ], [ 4, 5, 67 ] ]

Return:    (Array)

Example

  var arr = [1, 2, 2, 3, 4, 3];
  var ret = K.unique(arr);
  console.log(ret); // [1, 2, 3, 4]

Return:     (Array): the array by reverse

Example

  var arr = [2, 4, 6];
  var ret = K.reverse(arr);
  console.log(arr); // [2, 4, 6]

Return:    (Boolean): true or flase

Example

  var arr = [1, 2, 3];
  console.log(K.isPureArr(arr)); // true

  var arr2 = [1, "hello", true];
  console.log(K.isPureArr(arr2)); // false

Arg:    val(anyType): the element will be deleted

Return:    (Array)

Example

  var arr = [2, 'foo', false];
  console.log(K.delByVal(arr, false)); // [2, 'foo']

Arg:    index(Number): the index

Return:    (Array)

Example

  var arr = [1, 2, 3];
  console.log(K.delByIndex(arr, 2)); // [1, 2]

Reg:    val(Number): one of element in arr given

Return:    (Number)

Example

  var arr = [1, 2, 3];
  console.log(K.findIndex(arr, 2)); // 2

Rurn:    (Array)

Example

  var o = {foo: 'foo'};
  var o2 = {foo: 'foo'};
  var arr = [o, o2];
  console.log(K.uniqueArrForRef(arr)); // [{foo: 'foo'}]

Return:   (Array)

Example

  // arrIsObjLike
  var arr = [1, 2, 3];
  console.log(K.arrIsObjLike(arr)); // false

  var arr2 = [1, 2, 3];
  arr2.bar = 'bar';
  console.log(K.arrIsObjLike(arr2)); // true

Return:    (Array)

Example

  var arr = [1, 2, 'hello', 'boy', true];
  console.log(K.delFromLeft(arr,2)); // [ 'hello', 'boy', true ]
  console.log(K.delFromLeft(arr, 1)); // [ 'boy', true ]

Return:    (Array)

Example

  var arr = [1, 2, 'hello', 'boy', true];
  console.log(K.delFromRight(arr,2)); // [ 1, 2, 'hello' ]
  console.log(K.delFromRight(arr, 1)); // [ 1, 2 ]

Return:   (Number)

Example

  var arr = [1, 2, 3];
  console.log(K.sum(arr)); // 6

Arg:    len(Number): the length of arr you expect    min(Number): the min element of arr    max(Number): the max element of arr

Example

  console.log(K.generateRand()); // [2]
  console.log(K.generateRand(2, 2, 8)); // [ 4, 7 ]

Example

  var arr1 = [1, 2, 3];
  var arr2 = [2, 3, 4];
  console.log(K.union(arr1, arr2)); // [1, 2, 3, 4]

Example

  var arr1 = [1, 2, 3];
  var arr2 = [2, 3, 4];
  console.log(K.diff(arr1, arr2)); // [1, 4]

Example

  var arr = [1, 2, 3, 4, 5];
  console.log(K.shuffle(arr)); // [ 3, 1, 2, 4, 5 ]

note: just work on the arr which all elment is number type

Example

  var arr = [1, [2, [3, [4]]]];
  console.log(K.deepFlatten(arr)); // [1, 2, 3, 4];

Example

  var arr = [1, 2, 3, 4, 9, 5, 7, 7];
  console.log(K.findDup(arr)); // 7

Function related api:

Arg: fn(Function)

Example

  function F() {}

  console.log(K.isConstr(F)); // true

  console.log(K.isConstructor(Math)); // flase

Example

  
  function fn(){}
  console.log(K.fnIsObjLike(fn)); // false

  function fn2() {};
  fn2.bar = 'bar';
  console.log(K.fnIsObjLike(fn2)); // true

Object related api:

Example

  var o = {
    o2: {
      foo: 'foo',
    }
  };


  var obj = K.deepFreeze(o);
  obj.o2.foo = 'changed';
  console.log(obj.o2.foo); // foo

Example

  var obj = {
    '1': 'hello',
    '2': 'boy',
    'length': 2
  };
  console.log(K.isArrayLike(obj)); // true

Example

  var obj1 = {
    foo: 'hello',
    b: true
  };

  var obj2 = K.shallowCloneObj(obj1);
  console.log(obj2); // { foo: 'hello', b: true }
  console.log(obj2 === obj1); // flase

Return: (Number)

Example

  var obj = {
    gretting: 'hello'
  };
  console.log(K.getAllProp(obj));
  /*
    [ 'gretting',
      'constructor',
      '__defineGetter__',
      '__defineSetter__',
      'hasOwnProperty',
      '__lookupGetter__',
      '__lookupSetter__',
      'isPrototypeOf',
      'propertyIsEnumerable',
      'toString',
      'valueOf',
      '__proto__',
      'toLocaleString' ]
  */

Return: (Boolean)

Example

  var obj = {
    gretting: 'hello'
  };

  var obj2 = {
    gretting: 'hello'
  };
  console.log(K.shallowEqual(obj,obj2)); // true

Example

  var obj = {
    letter: 'a',
    obj2: {
      letter: 'b',
      obj3: {
        letter: 'c'
      }
    }
  };
  console.log(K.getNearProp(obj, 'letter')); // a

changeKeyByVal(obj, valOfProp, newProp)

change the property of obejct by the value provided

Example

  var obj = {
    foo: 'hello'
  };
  K.changeKeyByVal(obj, 'hello', 'bar');
  console.log(obj); // { bar: 'hello' }

getNestProp(obj, prop)

Example

  var obj = {
    obj2: {
      obj3: {
        a: 1
      }
    }
  };
  var a = K.getNearProp(obj, 'a');
  console.log(a); // 1

  var obj = {
    obj2: {
      obj3: {
        a: 1
      }
    }, 
    a: 2
  };

  var a2 = K.getNearProp(obj, 'a');
  console.log(a2); // 2
  // console.log(K.getNearProp(obj, 'foo')); // err: not found


  // example 2
  var obj = {
    foo: 'a',
    firstNest: {
      foo: 'b',
      secondNest: {
        foo: 'c',
        thirdNest: {
          foo: 'd'
        }
      }
    }
  };

  // default index is 1
  console.log(K.getNestProp(obj, 'foo')); // a
  console.log(K.getNestProp(obj, 'foo', 2)); // b
  console.log(K.getNestProp(obj, 'foo', 5)); // undefined

  // flag: if not find then return the last value
  console.log(K.getNestProp(obj, 'foo', 5, true)); // d
  console.log(K.getNestProp(obj, 'foo', 25, true)); // d

getPureObj()

get a object, which parent is null;

Example

  var obj = K.getPureObj();
  console.log(obj); // {}
  console.log(o.constructor); // Object
  console.log(obj.__proto__); // undefined

includeKey(obj, ...key)

Example

  var obj = {
    foo: 'hello',
    bar: 'boy'
  };

  console.log(K.includeKey(obj, 'foo')); // true
  console.log(K.includeKey(obj, 'bar')); // true
  console.log(K.includeKey(obj, 'foo', 'bar')); // true

  console.log(K.includeKey(obj, 'notFound')); // false

includeVal(obj, ...key)

Example

  var obj = {
    foo: 'hello',
    bar: 'boy'
  };

  console.log(K.includeVal(obj, 'hello')); // true
  console.log(K.includeVal(obj, 'notFound')); // false

mapKeyVal(keysArr, valuesArr)

Example

  var keys = ['num1', 'num2'];
  var values = [1, 2];
  console.log(K.mapKeyVal(keys, values)); // { num1: 1, num2: 2 }

getPropDes(obj, prop)

Example

  var obj = {
    gretting: 'hi'
  };

  var des = K.getPropDes(obj, 'gretting');
  console.log(des);

  /*
    { value: 'hi',
      writable: true,
      enumerable: true,
      configurable: true }
   */


  // example2
  var des2 = K.getAccessedPropDes(obj, '__proto__'); // get from Object.prototype
  console.log(des2);
  /*
    { get: [Function: get __proto__],
      set: [Function: set __proto__],
      enumerable: false,
      configurable: true }
   */

getKeyByVal(obj, val)

Example

  var obj = {
    boy: 'manx'
  };
  console.log(K.getKeyByVal(obj, 'manx')); // boy
1.0.0

6 years ago