mirror of
				https://github.com/flynx/types.js.git
				synced 2025-10-31 03:20:08 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			87 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /**********************************************************************
 | |
| * 
 | |
| *
 | |
| *
 | |
| **********************************************/  /* c8 ignore next 2 */
 | |
| ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
 | |
| (function(require){ var module={} // make module AMD/node compatible...
 | |
| /*********************************************************************/
 | |
| 
 | |
| var object = require('ig-object')
 | |
| 
 | |
| 
 | |
| 
 | |
| /*********************************************************************/
 | |
| 
 | |
| var SetProtoMixin =
 | |
| module.SetProtoMixin =
 | |
| object.Mixin('SetMixin', 'soft', {
 | |
| 	// Set set operation shorthands...
 | |
| 	unite: function(other=[]){ 
 | |
| 		return new Set([...this, ...other]) },
 | |
| 	intersect: function(other){
 | |
| 		var test = other.has ?  
 | |
| 			'has' 
 | |
| 			: 'includes'
 | |
| 		return new Set([...this]
 | |
| 			.filter(function(e){ 
 | |
| 				return other[test](e) })) },
 | |
| 	subtract: function(other=[]){
 | |
| 		other = new Set(other)
 | |
| 		return new Set([...this]
 | |
| 			.filter(function(e){ 
 | |
| 				return !other.has(e) })) },
 | |
| 
 | |
| 	sort: function(values=[]){
 | |
| 		values = (typeof(values) == 'function' 
 | |
| 				|| values === undefined) ?
 | |
| 			[...this].sort(values)
 | |
| 			: values
 | |
| 		var del = this.delete.bind(this)
 | |
| 		var add = this.add.bind(this)
 | |
| 		new Set([...values, ...this])
 | |
| 			.forEach(function(e){
 | |
| 				if(this.has(e)){
 | |
| 					del(e)
 | |
| 					add(e) } }.bind(this))
 | |
| 		return this },
 | |
| 
 | |
| 	replace: function(old, value, ordered=true){
 | |
| 		// nothing to do...
 | |
| 		if(!this.has(old) || old === value){
 | |
| 			return this }
 | |
| 		if(ordered){
 | |
| 			var order = [...this]
 | |
| 			// XXX is this fast enough???
 | |
| 			order[order.lastIndexOf(old)] = value }
 | |
| 		// replace...
 | |
| 		this.delete(old)
 | |
| 		this.add(value)
 | |
| 		ordered
 | |
| 			&& this.sort(order)
 | |
| 		return this },
 | |
| 	replaceIndex: function(index, value, ordered=true){
 | |
| 		// nothing to do...
 | |
| 		if(this.size < index || old === value){
 | |
| 			return this }
 | |
| 		var order = [...this]
 | |
| 		var old = order[index]
 | |
| 		ordered
 | |
| 			&& (order[index] = value)
 | |
| 		// replace...
 | |
| 		this.delete(old)
 | |
| 		this.add(value)
 | |
| 		ordered
 | |
| 			&& this.sort(order)
 | |
| 		return this },
 | |
| })
 | |
| 
 | |
| 
 | |
| SetProtoMixin(Set.prototype)
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| /**********************************************************************
 | |
| * vim:set ts=4 sw=4 :                               */ return module })
 |