package:uri_parser

🔗 A minimal & safe utility to parse URIs.

Install

Add in your pubspec.yaml.

dependencies:
  uri_parser: ^1.0.0

Example

Parse or Identify

final parser = URIParser('C:/Windows/System32');
print(parser.type);            // URIType.directory
print(parser.file);            // null
print(parser.directory);       // Directory: 'C:/Windows/System32'
print(parser.uri);             // null
final parser = URIParser('C:\Windows\explorer.exe');
print(parser.type);            // URIType.file
print(parser.file);            // File: 'C:/Windows/explorer.exe'
print(parser.directory);       // null
print(parser.uri);             // null
final parser = URIParser('file://C:/Windows/explorer.exe');
print(parser.type);            // URIType.file
print(parser.file);            // File: 'C:/Windows/explorer.exe'
print(parser.directory);       // null
print(parser.uri);             // null
final parser = URIParser('https://www.example.com/test');
print(parser.type);            // URIType.network
print(parser.file);            // null
print(parser.directory);       // null
print(parser.uri);             // https://www.example.com/test

Validate or Get Uri

final parser = URIParser('C:\Windows\explorer.exe');
final bool valid = parser.validate();
final Uri result = parser.result;

Why

First of all, this is built with requirements of Harmonoid in mind.

Being a music player, there's a great involvement of URIs everywhere. Since, users can give their inputs in wide range of formats, this is built to handle that while providing some finer control. Few being:

  • Handles both file:// URIs & File/Directory path as input.
  • Provides a way to distinguish between File, Directory & network URLs.
  • Correctly handles File paths with backward slashes on Windows.
  • Uri.parse & Uri.toFilePath do not handle file:// scheme (with two slashes) correctly on Windows.

License

Copyright © 2022, Hitesh Kumar Saini <saini123hitesh@gmail.com>

This project & the work under this repository is governed by MIT license that can be found in the LICENSE file.

Libraries

uri_parser
This file is a part of uri_parser (https://github.com/alexmercerind/uri_parser).