untildify

repository·main·Indexed 41 days ago

https://github.com/sindresorhus/untildify

A utility for converting tilde-prefixed paths (e.g., `~/dev`) into absolute paths based on the current user's home directory. Version 6.0.0 supports regular tilde expansion and username tilde expansion if the username matches the current user.

Tokens
590
Snippets
3
Records
3
Agent score
50%

What's inside untildify

  1. Convert tilde paths to absolute paths with untildify()

    main

    Use the untildify function to expand tilde (~) shortcuts into absolute file system paths.

    • ~ is expanded to the current user's home directory.
    • ~username is expanded to that user's home directory only if the username matches the current user. If the username does not match, the path is returned unchanged.
    import untildify from 'untildify';
    
    // Expands ~ to the current user's home directory
    untildify('~/dev');
    //=> '/Users/sindresorhus/dev'
    
    // Expands ~username if the username matches the current user
    untildify('~sindresorhus/dev');
    //=> '/Users/sindresorhus/dev'
  2. Use untildify to convert tilde paths to absolute paths

    main

    The untildify function converts paths starting with a tilde (~) into absolute paths.

    It supports two types of expansion:

    1. Regular tilde expansion (~): Replaces the tilde with the current user's home directory (retrieved via os.homedir()).
    2. Username tilde expansion (~username): If the username provided in the path matches the current user's username (retrieved via os.userInfo().username), it expands to that user's home directory. If the username does not match the current user, the path is returned unchanged.

    If the input is not a string, the function throws a TypeError.

    import untildify from 'untildify';
    
    // Regular tilde expansion
    untildify('~/Documents') // returns '/Users/name/Documents'
    
    // Username expansion (only if it matches current user)
    untildify('~name/Documents') // returns '/Users/name/Documents'
    
    // No expansion if username doesn't match
    untildify('~otheruser/Documents') // returns '~otheruser/Documents'