0.0.21 • Published 4 years ago

u3s v0.0.21

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

u3s.js NPM Package Build Size NPM Downloads Dev Dependencies

javascript micro-library of small λunctions written with typescript

english

Problem❓

no problem just wanting to practice

Solution❓

no problem, just having fun with programming functions which i will use into my personal projects as d2k.js.

⚠️ Disclaimer

I am not a developer, I am just a normal guy who really appreciates the programming wanting to improve his knowledge. I'm not a baws of typescript, let me know if you have some advices.

🛠️ Features

  • convert - contains functions for collection conversions.
  • deletion - contains functions to delete property or value from an object or array.
  • flatten - flats an array of matrix.
  • frozen - freezes an object deeply.
  • identity - returns the first argument of a function.
  • is - contains flag functions (is.array, is.object, is.empty, etc.).
  • iteration - contains iteration functions as forEach which loops on an array and executes the function passed as arguments.
  • merge - merges source objects into a target object.
  • oftype - returns the real type of an object (like typeof but more precise).
  • pipe - executes functions and executes them in a reduce, making the input parameter of the function that is called the result of the previous function.
  • ratio169 - returns the perfect ratio of a screen to apply a 16:9 dimension.
  • reducer - a small personalized reducer that a sensei taught me during my learning path.
  • report - contains functions for error handling.
  • strings - contains functions for string conversion.
  • ua - contains functions for the detection of browsers, tablets and mobiles.
  • uniqid - creates an uuid

📦 Install dependencies

Command line

npm i u3s

OR

yarn add u3s

Download

You can also download this repo, to recover the file located in build/u3s.js and then go and place it in your application in the place provided for this purpose.

🚀 Start project

import u3s from 'u3s'; // global

OR

import { convert } from 'u3s'; // specific

📖 API

well it's cool but how does it work?

  • u3s.convert

    contains functions for conversion of collections.

    • .makeKeysIntoArray( payload )

      converts the keys of an object into an array containing the keys of the object.

      params

      payload { Object }: collection.
      returns { Array }: an array n keys.

      example
      u3s.convert.makeKeysIntoArray( { trap: 'skurt skurt', stars: 1000000000 } );
      
      // output: [ 'trap', 'stars' ]
  • u3s.deletion

    contains functions to delete property or value from an object or array.

    • .makeRemoveProperty( payload, query )

      converts the keys of an object into an array containing the keys of the object.

      params

      payload { Object }: collection.
      query { Object }: a search query.
      returns { void }: undefined.

      example
      u3s.convert.makeRemoveProperty( {
        artist: 'takeoff',
        genre: 'trap',
        stars: 1000000000,
        title: '24/7',
        url: 'https://www.youtube.com/watch?v=ThotL18UkJo'
      }, [ 'genre', 'stars' ] );
      
      // output: { artist: 'takeoff', title: '24/7', url: 'https://www.youtube.com/watch?v=ThotL18UkJo' }
    • .makeRemoveValues( payload, query )

      converts the keys of an object into an array containing the keys of the object.

      params

      payload { Object }: collection.
      query { Object }: a search query.
      returns { void }: undefined.

      example
      u3s.convert.makeRemoveValues( [
        'takeoff',
        'trap',
        1000000000,
        '24/7',
        'https://www.youtube.com/watch?v=ThotL18UkJo'
      ], 'https://www.youtube.com/watch?v=ThotL18UkJo' );
      
      // output: [ 'takeoff', 'trap', 1000000000, '24/7' ]
    • .makeReset( payload )

      converts the keys of an object into an array containing the keys of the object.

      params

      payload { Object }: collection.
      query { Object }: a search query.
      returns { void }: undefined.

      example
      u3s.convert.makeReset( [
        'takeoff',
        'trap',
        1000000000,
        '24/7',
        'https://www.youtube.com/watch?v=ThotL18UkJo'
      ] );
      
      // output: []
  • u3s.flatten

    flats an array of matrix.

    params

    payload { Array|Object }: collection.
    returns { Array|Object }: an object frozen deep.

    example
    const data = [ 1, 2, [ 3, 4 ], [ 5, [ 6, 7, [ 8, 9 ] ] ] ];
    
    u3s.flatten( data );
    
    // output: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  • u3s.frozen( payload )

    freezes an object at all levels.

    params

    payload { Array | Object }: collection.
    returns { Array | Object }: an object frozen deep.

    example
    const data = { a: 'a', b: 'b', c: { c2: 'c2', c3: { c4: 'c4' } } };
    
    u3s.frozen( data );
    
    Object.isFrozen( data.c.c2 );
    
    // output: true
    
    Object.isFrozen( data.c.c2.c3.c4 );
    
    // output: true
  • u3s.identity

    returns the first argument of a function.

    params

    payload { * }: any.
    returns { * }: the first argument.

    example
    u3s.identity( { artist: 'rich the kid', title: 'easy' } );
    
    // output: { artist: 'rich the kid', title: 'easy' }
  • u3s.is

    it contains flagger functions.

    • .array( payload )

      check if the payload is an array.

      params

      payload { * }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.array( [] ); // true
    • .bool( payload )

      check if the payload is a boolean.

      params

      payload { * }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.bool( true ); // true
    • .contain( payload )

      check if the payload contains a value.

      params

      payload { * }: any
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.contain( { a: 'a' } );
      
      // output: true
      
      u3s.is.contain( [ 'yo', 'snoop' ] );
      
      // output: true
    • .date( payload )

      check if the payload contains a value.

      params

      payload { * }: any
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.date( { a: 'a' } );
      
      // output: false
      
      u3s.is.date( [ 'yo', 'snoop' ] );
      
      // output: true
    • .empty( payload )

      check if the payload do not contains a value.

      params

      payload { * }: any.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.empty( null );
      u3s.is.empty( undefined );
      u3s.is.empty( [] );
      u3s.is.empty( {} );
      u3s.is.empty( true );
      u3s.is.empty( () => {} );
      
      // output: true
      
      u3s.is.empty( { a: 'a' } );
      u3s.is.empty( [ 'yo', 'snoop' ] );
      
      // output: false
    • .error( payload )

      it checks if payload is an error.

      params

      payload { * }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.error( null );
      
      // output: false
      
      u3s.is.error( new Error() );
      
      // output: true
    • .include( payload, query )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array | Object }: collection.
      query { String }: the value to search for. returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.include( [ 'travis', 'kendrick', 'chainz' ], '' );
      
      // output: false
      
      u3s.is.include( [ 'travis', 'kendrick', 'chainz' ], 'travis' );
      
      // output: true
    • .method( payload, query )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array|Object }: collection.
      query { String }: the value to search for. returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.method( {} );
      
      // output: false
      
      u3s.is.method( () => {} );
      
      // output: true
    • .number( payload )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { * }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.number( {} );
      
      // output: false
      
      u3s.is.number( 1e-4 );
      
      // output: true
    • .regex( payload )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array|Object }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.regex( {} );
      
      // output: false
      
      u3s.is.regex( /W/g );
      
      // output: true
    • .string( payload )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array|Object }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.string( {} );
      
      // output: false
      
      u3s.is.string( 'bedroom galaxy' );
      
      // output: true
    • .object( payload )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array|Object }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.object( [] );
      
      // output: false
      
      u3s.is.object( { count: 0 } );
      
      // output: true
    • .johndoe( payload )

      it checks if payload is a valid parameters then it checks if the query is includes also then it returns a boolean value.

      params

      payload { Array|Object }: a value.
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.is.johndoe( 'flying dragon' );
      
      // output: false
      
      u3s.is.johndoe( null );
      u3s.is.johndoe( undefined );
      
      // output: true
  • u3s.iteration( payload, f )

    a simple forEach under the hood which loops over the elements of an array and executes the function passed in parameters.

    params

    payload { Array }: collection.
    f { Function }: callback.
    returns { Object }: it loops an array and calls the callback function with the current value into it.

    example
    u3s.is.iteration( [ 'a', 'b', 'c' ], ( value, index, array ) => console.log( value, index, array ) );
    
    // output: 'a', 0, [ 1, 2, 3 ]
    // output: 'b', 1, [ 1, 2, 3 ]
    // output: 'c', 2, [ 1, 2, 3 ]
  • u3s.merge( o1, o2 )

    merge two objects into one. only the direct properties of an object will be copied.

    params

    o1 { Object }: collection.
    o2 { Object }: collection.
    returns { Object }: a single one merged object.

    example
    u3s.merge( { a: 'a' }, { b: 'b' } );
    
    // output: { a: 'a', b: 'b' }
  • u3s.oftype( payload )

    returns the true type of an object ( like typeof but more precise ).

    params

    payload { * }: any.
    returns { String }: the type of an object.

    example
    u3s.oftype( '' );
    
    // output: string
    
    u3s.oftype( [] );
    
    // output: array
  • u3s.pipe( ...fs )( payload )

    Processes an array of functions and executes them in a reduce, ensuring that the input parameter of the function that is called is the result of the previous function.

    params

    fs { Array<Function> }: list.
    returns { Object }: the value of the reduce method.

    example
    u3s.pipe( payload => Object.assign( payload, { a: 'a' } ) )( { o: 'o' } );
    
    // output: { o: 'o', a: 'a' }
  • u3s.ratio169( width )

    returns the perfect ratio in 16:9.

    params

    width { Number }: integer.
    returns { Number }: the 16:9 ratio.

    example
    u3s.ratio( 700 );
    
    // output: 394
  • u3s.reducer( payload, f )

    a small custom reducer that a sensei taught me during my learning path. this will allow you to use the reduce method from an object passed as a function argument.

    params

    payload { Object }: collection.
    f { Function }: callback.
    returns { Object }: the value of the reduce method.

    example
    u3s.reducer( { a: 'a', b: 'b', c: 'c' }, payload => payload = [] );
    
    // output: { a: [], b: [], c: [] }
  • u3s.report

    contains functions for error handling.

    • .required

      returns an error type or an error with a predefined message.

      example
      u3s.report.required
      
      // output: new Error()
      
      u3s.report.required = 'listenning "bedroom galaxy" is required.';
      
      // output: new Error( 'listenning "bedroom galaxy" is required.' )
    • .warning

      returns an warning with a predefined message.

      example
      u3s.report.warning = 'wilow amsgood - "bedroom galaxy" is required.';
      
      // output: 'wilow amsgood - "bedroom galaxy" is required.' via console.warn
  • u3s.ua

    contains detection functions provided by the user agent.

    • .android()

      detects if the os is an "android"

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.android();
      
      // output: true / false
    • .browser()

      detects if the os is a "browser"

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.browser();
      
      // output: true / false
    • .edge()

      detects if it is the browser "edge".

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.edge();
      
      // output: true / false
    • .ie()

      detects if it is the browser "ie".

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.ie();
      
      // output: true / false
    • .ios()

      detects if the OS is an "ios".

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.ios();
      
      // output: true / false
    • .phone()

      detects if the os is that of a smartphone type device.

      params

      no params
      returns { Boolean }: true / false, as appropriate.

      example
      u3s.ua.phone();
      
      // output: true / false
  • u3s.uniqid()

    generate a uuid.

    params

    no params
    returns { String }: an alphanumeric character string.

    example
    u3s.uniqid();
    
    // output: ca18382c-2eef-d8da-8394-70b360e4d44d

🚨 Tests

tests are not up to date..

Running

reports

npm run test:reports

OR

yarn test:reports

watch

npm run test:watch

OR

yarn test:watch

Coverage

File% Stmts% Branch% Funcs% LinesUncovered Line #s
All files0000
u3s.convert.ts0000
u3s.deletion.ts0000
u3s.flatten.ts0000
u3s.frozen.ts0000
u3s.identity.ts0000
u3s.is.ts0000
u3s.iteration.ts0000
u3s.merge.ts0000
u3s.oftype.ts0000
u3s.pipe.ts0000
u3s.ratio169.ts0000
u3s.reducer.ts0000
u3s.report.ts0000
u3s.strings.ts0000
u3s.ua.ts0000
u3s.uniqid.ts0000

📝 Todo

  • improve unit testing
  • add more functions
  • documentation
  • jsfiddle examples

©️ License

Copyright ©️ 2020 monsieurbadia

Released under the MIT license

🙏 Supports

logo by @mllemartins with 🖤
code by @monsieurbadia with 🖤

⭐️ this repository if this project helped you!

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.16

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago