Skip to content

Commit

Permalink
Expose a few functions for public use (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
airxnoor authored Apr 5, 2024
1 parent c7c09ee commit 065722b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.2.2 - April 3, 2024
• Expose a few functions to public use.

0.2.1 - April 3, 2024
• Add a method to delete a log file.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

rootProject.group = "com.airthings.lib"
rootProject.version = "0.2.1"
rootProject.version = "0.2.2"

buildscript {
repositories {
Expand Down
52 changes: 39 additions & 13 deletions src/commonMain/kotlin/com/airthings/lib/logging/LogDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

@file:Suppress("MagicNumber")
@file:Suppress("unused", "MemberVisibilityCanBePrivate")

package com.airthings.lib.logging

Expand All @@ -35,7 +35,7 @@ data class LogDate(
val year: Int,
val month: Int,
val day: Int,
val separator: Char = '-',
val separator: Char = SEPARATOR,
) {
/**
* Returns a [LogDate] instance from a [LocalDateTime] component.
Expand All @@ -59,13 +59,21 @@ data class LogDate(

companion object {
/**
* The character used to separate date parts, f.ex: `2023-08-23`.
* The default character used to separate date parts, f.ex: `2023-08-23`.
*/
const val SEPARATOR = '-'

internal const val LOG_FILE_LENGTH_WITHOUT_SEPARATOR = 8
internal const val LOG_FILE_LENGTH_WITH_SEPARATOR = 10
}
}

internal fun LogDate.toString(separator: Char?): String = StringBuilder(10)
/**
* Returns the string representation of this instance, optionally separated by a character.
*
* @param separator Optional separator between the file name's components.
*/
fun LogDate.toString(separator: Char?): String = StringBuilder(LogDate.LOG_FILE_LENGTH_WITH_SEPARATOR)
.apply {
append(year)
if (separator != null) {
Expand All @@ -79,38 +87,56 @@ internal fun LogDate.toString(separator: Char?): String = StringBuilder(10)
}
.toString()

internal fun String.asLogDate(separator: Char?): LogDate? {
val expectedLength = if (separator == null) 8 else 10
/**
* Converts this compatible string to a [LogDate] on success, null otherwise.
*
* @param separator Optional separator between the file name's components.
*/
@Suppress("MagicNumber")
fun String.asLogDate(separator: Char?): LogDate? {
val expectedLength = if (separator == null) {
LogDate.LOG_FILE_LENGTH_WITHOUT_SEPARATOR
} else {
LogDate.LOG_FILE_LENGTH_WITH_SEPARATOR
}
if (expectedLength != length) {
return null
}

val intValue = (if (separator == null) {
val intValue = if (separator == null) {
toIntOrNull()
} else {
replace("$separator", "").toIntOrNull()
}) ?: return null
}

intValue ?: return null

val dateValue = "$intValue"
if (dateValue.length != 8) {
if (dateValue.length != LogDate.LOG_FILE_LENGTH_WITHOUT_SEPARATOR) {
return null
}

val year = dateValue.substring(0, 4).toIntOrNull() ?: return null
val month = dateValue.substring(4, 6).toIntOrNull() ?: return null
val day = dateValue.substring(6, 8).toIntOrNull() ?: return null
val day = dateValue.substring(6, LogDate.LOG_FILE_LENGTH_WITHOUT_SEPARATOR).toIntOrNull() ?: return null

return LogDate(year, month, day)
}

internal fun LogDate.after(another: LogDate): Boolean = year > another.year ||
/**
* Returns true if this [LogDate] is newer than [another], false otherwise.
*
* @param another The other [LogDate] instance to compare against.
*/
fun LogDate.after(another: LogDate): Boolean = year > another.year ||
year == another.year && month > another.month ||
month == another.month && day > another.day

/**
* Returns true if this string denotes a log file created after [date], or if [date] is null, false otherwise.
* Returns true if this string denotes a log file created after [date], or if [date] is null,
* false otherwise.
*/
internal fun String.ifAfter(date: LogDate?): Boolean {
fun String.ifAfter(date: LogDate?): Boolean {
val fileNameWithoutExtension = substringBeforeLast('.')
val logDate = fileNameWithoutExtension.asLogDate(date?.separator ?: LogDate.SEPARATOR)

Expand Down

0 comments on commit 065722b

Please sign in to comment.