CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
maps.mjs115 linesDownload Raw Back to util
1import * as is from '../is.mjs';2import { extend } from './extend.mjs';3 4  // has anything been set in the map5export const mapEmpty = map => {6  let empty = true;7 8  if( map != null ){9    return Object.keys( map ).length === 0;10  }11 12  return empty;13};14 15// pushes to the array at the end of a map (map may not be built)16export const pushMap = options => {17  let array = getMap( options );18 19  if( array == null ){ // if empty, put initial array20    setMap( extend( {}, options, {21      value: [ options.value ]22    } ) );23  } else {24    array.push( options.value );25  }26};27 28// sets the value in a map (map may not be built)29export const setMap = options => {30  let obj = options.map;31  let keys = options.keys;32  let l = keys.length;33 34  for( let i = 0; i < l; i++ ){35    let key = keys[ i ];36 37    if( is.plainObject( key ) ){38      throw Error( 'Tried to set map with object key' );39    }40 41    if( i < keys.length - 1 ){42 43      // extend the map if necessary44      if( obj[ key ] == null ){45        obj[ key ] = {};46      }47 48      obj = obj[ key ];49    } else {50      // set the value51      obj[ key ] = options.value;52    }53  }54};55 56// gets the value in a map even if it's not built in places57export const getMap = options => {58  let obj = options.map;59  let keys = options.keys;60  let l = keys.length;61 62  for( let i = 0; i < l; i++ ){63    let key = keys[ i ];64 65    if( is.plainObject( key ) ){66      throw Error( 'Tried to get map with object key' );67    }68 69    obj = obj[ key ];70 71    if( obj == null ){72      return obj;73    }74  }75 76  return obj;77};78 79// deletes the entry in the map80export const deleteMap = options => {81  let obj = options.map;82  let keys = options.keys;83  let l = keys.length;84  let keepChildren = options.keepChildren;85 86  for( let i = 0; i < l; i++ ){87    let key = keys[ i ];88 89    if( is.plainObject( key ) ){90      throw Error( 'Tried to delete map with object key' );91    }92 93    let lastKey = i === options.keys.length - 1;94    if( lastKey ){95 96      if( keepChildren ){ // then only delete child fields not in keepChildren97        let children = Object.keys( obj );98 99        for( let j = 0; j < children.length; j++ ){100          let child = children[j];101 102          if( !keepChildren[ child ] ){103            obj[ child ] = undefined;104          }105        }106      } else {107        obj[ key ] = undefined;108      }109 110    } else {111      obj = obj[ key ];112    }113  }114};115 
basant307/AI_Governance_Project · CoolFace