Skip to content

Commit 24806d2

Browse files
Rename to uniqueId and add private methods
1 parent 2e426a2 commit 24806d2

File tree

10 files changed

+59
-22
lines changed

10 files changed

+59
-22
lines changed

templates/android/library/src/main/java/io/appwrite/ID.kt.twig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import java.time.Instant
44
import kotlin.math.floor
55
import kotlin.random.Random
66

7-
fun uniqid(): String {
7+
// Generate a unique ID based on timestamp
8+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9+
private fun uniqueId(): String {
810
val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
911
val secondsPart = floor(currentTimestamp).toLong()
1012
val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
@@ -18,8 +20,11 @@ class ID {
1820
companion object {
1921
fun custom(id: String): String
2022
= id
23+
24+
// Generate a unique ID with padding to have a longer ID
25+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2126
fun unique(padding: Int = 7): String {
22-
val baseId = uniqid()
27+
val baseId = uniqueId()
2328
val randomPadding = (1..padding)
2429
.map { Random.nextInt(0, 16).toString(16) }
2530
.joinToString("")

templates/dart/lib/id.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ID {
66

77
// Generate a unique ID based on timestamp
88
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9-
static String _uniqid() {
9+
static String _uniqueId() {
1010
final now = DateTime.now();
1111
final secondsSinceEpoch = (now.millisecondsSinceEpoch / 1000).floor();
1212
final msecs = now.microsecondsSinceEpoch - secondsSinceEpoch * 1000000;
@@ -17,7 +17,7 @@ class ID {
1717
// Generate a unique ID with padding to have a longer ID
1818
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1919
static String unique({int padding = 7}) {
20-
String id = _uniqid();
20+
String id = _uniqueId();
2121

2222
if (padding > 0) {
2323
StringBuffer sb = StringBuffer();

templates/deno/src/id.ts.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export class ID {
2-
static uniqid(): string {
2+
// Generate a unique ID based on timestamp
3+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
4+
static #uniqueId(): string {
35
const now = new Date();
46
const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
57
const msecs = now.getTime() - secondsSinceEpoch * 1000;
@@ -14,8 +16,10 @@ export class ID {
1416
return id
1517
}
1618

19+
// Generate a unique ID with padding to have a longer ID
20+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1721
public static unique(padding: number = 7): string {
18-
const baseId = ID.uniqid();
22+
const baseId = ID.#uniqueId();
1923
let randomPadding = '';
2024

2125
for (let i = 0; i < padding; i++) {

templates/dotnet/src/Appwrite/ID.cs.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ namespace {{ spec.title | caseUcfirst }}
44
{
55
public static class ID
66
{
7-
static string UniqID()
7+
// Generate a unique ID based on timestamp
8+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9+
private static string uniqueId()
810
{
911
var now = DateTime.UtcNow;
1012
var secondsSinceEpoch = (long)(now - new DateTime(1970, 1, 1)).TotalSeconds;
@@ -16,10 +18,12 @@ namespace {{ spec.title | caseUcfirst }}
1618
return hexTimestamp;
1719
}
1820

21+
// Generate a unique ID with padding to have a longer ID
22+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1923
public static string Unique(int padding = 7)
2024
{
2125
var random = new Random();
22-
var baseId = UniqID();
26+
var baseId = uniqueId();
2327
var randomPadding = "";
2428

2529
for (int i = 0; i < padding; i++)

templates/kotlin/src/main/kotlin/io/appwrite/ID.kt.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import java.time.Instant
44
import kotlin.math.floor
55
import kotlin.random.Random
66

7-
fun uniqid(): String {
7+
// Generate a unique ID based on timestamp
8+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
9+
private fun uniqueId(): String {
810
val currentTimestamp = Instant.now().toEpochMilli() / 1000.0
911
val secondsPart = floor(currentTimestamp).toLong()
1012
val microsecondsPart = ((currentTimestamp - secondsPart) * 1_000_000).toLong()
@@ -18,8 +20,10 @@ class ID {
1820
companion object {
1921
fun custom(id: String): String
2022
= id
23+
// Generate a unique ID with padding to have a longer ID
24+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
2125
fun unique(padding: Int = 7): String {
22-
val baseId = uniqid()
26+
val baseId = uniqueId()
2327
val randomPadding = (1..padding)
2428
.map { Random.nextInt(0, 16).toString(16) }
2529
.joinToString("")

templates/node/lib/id.js.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class ID {
2-
static uniqid = () => {
2+
// Generate a unique ID based on timestamp
3+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
4+
static #uniqueId = () => {
35
const now = new Date();
46
const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
57
const msecs = now.getTime() - secondsSinceEpoch * 1000;
@@ -10,8 +12,10 @@ class ID {
1012
return hexTimestamp;
1113
}
1214

15+
// Generate a unique ID with padding to have a longer ID
16+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1317
static unique = (padding = 7) => {
14-
const baseId = ID.uniqid();
18+
const baseId = ID.#uniqueId();
1519
let randomPadding = '';
1620

1721
for (let i = 0; i < padding; i++) {

templates/python/package/id.py.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ from datetime import datetime
22
import random
33

44
class ID:
5+
// Generate a unique ID based on timestamp
6+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
57
@staticmethod
6-
def uniqid():
8+
def __uniqueId():
79
now = datetime.now()
810
seconds_since_epoch = int(now.timestamp())
911
microseconds_part = now.microsecond
@@ -14,8 +16,10 @@ class ID:
1416
def custom(id):
1517
return id
1618

19+
// Generate a unique ID with padding to have a longer ID
20+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1721
@staticmethod
1822
def unique(padding = 7):
19-
base_id = ID.uniqid()
23+
base_id = ID.__uniqueId()
2024
random_padding = ''.join(random.choice('0123456789abcdef') for _ in range(padding))
2125
return base_id + random_padding

templates/ruby/lib/container/id.rb.twig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'securerandom'
22

3-
def uniqid
3+
// Generate a unique ID based on timestamp
4+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
5+
def uniqueId
46
now = Time.now
57
seconds_since_epoch = now.to_i
68
microseconds_part = now.usec
@@ -15,7 +17,7 @@ module {{spec.title | caseUcfirst}}
1517
end
1618

1719
def self.unique(padding=7)
18-
base_id = uniqid
20+
base_id = uniqueId
1921
random_padding = SecureRandom.hex(padding)
2022
random_padding = random_padding[0...padding]
2123
base_id + random_padding

templates/swift/Sources/ID.swift.twig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Foundation
22

3-
func uniqid() -> String {
3+
// Generate a unique ID based on timestamp
4+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
5+
func uniqueId() -> String {
46
let now = Date()
57
let secondsSinceEpoch = Int(now.timeIntervalSince1970)
68
let microsecondsPart = Int((now.timeIntervalSince1970 - Double(secondsSinceEpoch)) * 1_000_000)
@@ -13,9 +15,13 @@ public class ID {
1315
return id
1416
}
1517

18+
// Generate a unique ID with padding to have a longer ID
19+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
1620
public static func unique(padding: Int = 7) -> String {
17-
let baseId = uniqid()
18-
let randomPadding = (1...padding).map { _ in String(format: "%x", Int.random(in: 0..<16)) }.joined()
21+
let baseId = uniqueId()
22+
let randomPadding = (1...padding).map {
23+
String(format: "%x", Int.random(in: 0..<16))
24+
}.joined()
1925
return baseId + randomPadding
2026
}
2127
}

templates/web/src/id.ts.twig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
function uniqid(): string {
1+
// Generate a unique ID based on timestamp
2+
// Recreated from https://www.php.net/manual/en/function.uniqid.php
3+
function uniqueId(): string {
24
const now = new Date();
3-
const secondsSinceEpoch = Math.floor(now.getTime() / 1000);
5+
const secondsSinceEpoch = Math.floor(now.getTime() / 1000); // Get Unix Timestamp
46
const microsecondsPart = now.getMilliseconds() * 1000; // Convert milliseconds to microseconds
57
const hexTimestamp = `${secondsSinceEpoch.toString(16)}${microsecondsPart.toString(16).padStart(5, '0')}`;
68
return hexTimestamp;
@@ -12,7 +14,9 @@ export class ID {
1214
}
1315

1416
public static unique(padding: number = 7): string {
15-
const baseId = uniqid();
17+
// Generate a unique ID with padding to have a longer ID
18+
// Recreated from https://github.com/utopia-php/database/blob/main/src/Database/ID.php#L13
19+
const baseId = uniqueId();
1620
let randomPadding = '';
1721
for (let i = 0; i < padding; i++) {
1822
const randomHexDigit = Math.floor(Math.random() * 16).toString(16);

0 commit comments

Comments
 (0)