jLinux

Untitled

Dec 27th, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const _      = require( 'lodash' );
  4. const lodash = _.runInContext();
  5.  
  6. const mixins = {
  7.     /**
  8.      * Return a new array containing only the unique objects inside the provided
  9.      * array. Unlike _.uniq, this will check _every_ key/value in the array
  10.      *
  11.      * @param   {Array}     arr     Array of structurally identical objects
  12.      * @return  {Array}
  13.      * @example
  14.      *
  15.      * const objs = [ { x: 1, y: 2 }, { a: 1, b: 2 }, { x: 1, y: 2 }];
  16.      *
  17.      * console.log( _( objs ).uniqObjs().value() )
  18.      * console.log( _.uniqObjs( objs ) )
  19.      *
  20.      * // => [ { x: 1, y: 2 }, { a: 1, b: 2 } ]
  21.      */
  22.     uniqObjs: arr => {
  23.         // Make sure that the arr parameter is a defined & populated array of objects
  24.         if( ! lodash.isArray( arr ) || ! arr.length || ! lodash.isObject( arr[0] ) )
  25.             return false;
  26.  
  27.         const uniqs = [];
  28.  
  29.         // Filter out the duplicate objects within the array by checking if
  30.         // the stringified object value already exist in the temporary uniqs
  31.         // array (while adding them to the variable)
  32.         return lodash.filter( arr, ( obj ) => {
  33.             // Use _.sortObj to sort the contents of the object by the keys, since stringify
  34.             // will use the current order (which means identical objects in different orders
  35.             // will be seen as discrepancies)
  36.             if( lodash.indexOf( uniqs, JSON.stringify( mixins.sortObj( obj ) ) ) === -1 ){
  37.                 uniqs.push( JSON.stringify( mixins.sortObj( obj ) ) );
  38.                 return true;
  39.             }
  40.  
  41.             return false;
  42.         });
  43.     },
  44.  
  45.     /**
  46.      * Return a copy of the object with the content sorted by the keys
  47.      *
  48.      * @param   {Object}    obj         Object to sort by keys
  49.      * @param   {Function}  comparator  Function to compare/sort the elements
  50.      * @return  {Object}
  51.      * @example
  52.      *
  53.      * const obj = {b: 3, c: 2, a: 1};
  54.      *
  55.      * console.log( _.sortObj( obj ) );
  56.      * console.log( _( obj ).sortObj().value() );
  57.      *
  58.      * // => {a: 1, b: 3, c: 2}
  59.      *
  60.      * _.sortObj(obj, (value, key) => {
  61.      *      return value;
  62.      * });
  63.      *
  64.      * // => {a: 1, c: 2, b: 3}
  65.      *
  66.      */
  67.     sortObj: ( obj, comparator ) => {
  68.         const keys = lodash.sortBy( lodash.keys( obj ), key => {
  69.             return lodash.isFunction( comparator ) ? comparator( obj[ key ], key ) : key;
  70.         });
  71.  
  72.         return lodash.object( keys, lodash.map( keys, key => {
  73.             return obj[ key ];
  74.         }));
  75.     },
  76.  
  77.     /**
  78.      * Check if the provided number is a float or integer value. This just tacks
  79.      * a 2nd check onto lodashes isNumber, which uses a lenient comparative operator
  80.      * to check if the value of parseFloat is the same as the provided number
  81.      *
  82.      * @param   {String|Integer|Number}  num     Number to check
  83.      * @return  {Boolean}
  84.      * @example
  85.      *
  86.      * _.isNumber( 123   )
  87.      * _.isNumber( '123' )
  88.      * _.isNumber( 1.2   )
  89.      * _.isNumber( '1.2' )
  90.      *
  91.      * // => true
  92.      *
  93.      */
  94.     isNumber: ( num ) => {
  95.         return lodash.isNumber( num ) || parseFloat( num ) == num;
  96.     }
  97. };
  98.  
  99. _.mixin( mixins );
  100.  
  101. module.exports = _;
Add Comment
Please, Sign In to add comment