UNPKG

cluedin-widget

Version:

This is the project for creating and managing widgets in CluedIn.

63 lines (58 loc) 1.68 kB
var compact = require( 'lodash/compact' ); var first = require( 'lodash/head' ); var rest = require( 'lodash/rest' ); var uniq = require( 'lodash/uniq' ); var without = require( 'lodash/without' ); var take = require( 'lodash/take' ); var takeRight = require( 'lodash/takeRight' ); var removeDuplicate = function( collection, keyname ) { var output = [], keys = []; collection.forEach( function( item ) { var key = item[ keyname ]; if ( keys.indexOf( key ) === -1 ) { keys.push( key ); output.push( item ); } } ); return output; }; var sortByName = function( a, b ) { if ( a.name < b.name ) { return -1; } if ( a.name > b.name ) { return 1; } return 0; }; module.exports = { removeDuplicate: removeDuplicate, sortByName: function( array ) { return array.sort( sortByName ); }, compact: compact, first: first, rest: rest, uniq: uniq, without: without, take: take, takeRight: takeRight, fill: function( value, len ) { if ( len == 0 ) return []; var a = [ value ]; while( a.length * 2 <= len ) a = a.concat( a ); if ( a.length < len ) a = a.concat( a.slice( 0, len - a.length ) ); return a; }, move: function( arr, old_index, new_index ) { if ( new_index >= arr.length ) { var k = new_index - arr.length; while( (k--) + 1 ) { arr.push( undefined ); } } arr.splice( new_index, 0, arr.splice( old_index, 1 )[ 0 ] ); return arr; // for testing purposes } };